locked
Specfifying drive letter when attaching a VHD using diskpart on Hyper-V Server 2008 R2 RRS feed

  • Question

  • I can successfully attach a virtual hard drive in Hyper-V Server 2008 R2 like so:

    diskpart
    DISKPART> select vdisk file="C:\Users\Public\Documents\Hyper-V\Virtual hard disks\my.vhd"
    DISKPART> attach vdisk

    In this case, the VHD is assigned the next available drive letter (which differs from time to time).

    Now I want to refer to the virtual disk by drive letter in a batch file. For my batch file to work consistently, I need to ensure that the virtual disk is always assigned the same drive letter.

    How can I specify what drive letter is assigned to the VHD?

    Thanks,
    Richard Koett.
     

    Wednesday, June 13, 2012 6:20 PM

Answers

  • Like it says "Please select a volume and try again". Before you issue the assign letter command do a list disk command and see what disk number your .vhd has. Issue select disk (your vhd disk number), then list volume. After you have your volume number type select volume (your volume number). Now issue the command to assign the letter. Let me know how it works.

    Adrian Costea - MCP, MCTS, MCSA 2003, MCITP: Windows 7

    My Blog: www.vkernel.ro/blog

    • Marked as answer by Richard Koett Thursday, June 14, 2012 1:35 AM
    Wednesday, June 13, 2012 8:36 PM

All replies

  • Here you go, see if it works for you Mounting VHD Files with DiskPart

    Adrian Costea - MCP, MCTS, MCSA 2003, MCITP: Windows 7

    My Blog: www.vkernel.ro/blog

    Wednesday, June 13, 2012 6:51 PM
  • Hi Adrian:

    That does not work for me. Results are as follows:

    DISKPART>select vdisk file="C:\Users\Public\Documents\Hyper-V\Virtual hard disks\my.vhd"
    DiskPart successfully selected the virtual disk file.

    DISKPART>attach vdisk
    100 perecent completed
    Diskpart successfully attached the virtual disk file.

    DISKPART>assign letter=V
    There is no volume specified.
    Please select a volume and try again.

    Any further suggestions?

    Thanks,
    Richard.

    Wednesday, June 13, 2012 7:42 PM
  • Like it says "Please select a volume and try again". Before you issue the assign letter command do a list disk command and see what disk number your .vhd has. Issue select disk (your vhd disk number), then list volume. After you have your volume number type select volume (your volume number). Now issue the command to assign the letter. Let me know how it works.

    Adrian Costea - MCP, MCTS, MCSA 2003, MCITP: Windows 7

    My Blog: www.vkernel.ro/blog

    • Marked as answer by Richard Koett Thursday, June 14, 2012 1:35 AM
    Wednesday, June 13, 2012 8:36 PM
  • Hi Adrian:

    Yes, I can assign the drive letter interactively by following the output of those commands.

    One thing I notice - after sucessfully assiging a drive letter, if I detach the VHD and then attach it again later, the drive letter is remembered. I don't need to use "assign" again.

    Now I need to figure out how to do all this from my batch file. It's easy enough to use a static script with diskpart, but now it appears I need to parse output from diskpart to figure out how many drives / volumes are attached and how they're numbered. I may end up having to resort to Powershell instead of my simple batch files.

    Thanks for helping,
    Richard.

    Thursday, June 14, 2012 1:34 AM
  • Hey Richard,

    Did you ever work it out? I am trying to create an automated provisioning script that will create a file (that will set IP address / hostname) and then input it to a vhdx (which is a sysprep image that should then boot and set its hostname and IP based on the autorun script)

    The issue I have is that I don't know how to ensure my script is using the correct drive letter. If two people run the script at the same time, its going to get messy.

    It is really annoying you cannot set the mountpoint/drive letter in diskpart when attaching the vhd. This means I would have to code the script to select a certain volume, however as mentioned above, if two people are running the same script, it will mess things up.

    I wonder if the information from  Get-DiskImage -ImagePath can be used to filter currently attached VHDs and then you can make a selection based on this.

    Thursday, December 20, 2012 11:00 AM
  • Kiwizz:

    I haven't been able to specify a drive letter when mounting a VHD file, but I made some progress on determining what drive letter has been assigned for me. Powershell 3 has new cmdlets that make this quite simple:

    http://richardspowershellblog.wordpress.com/2012/09/11/finding-the-drive-letter-of-a-mounted-vhd/

    If you're stuck using Powershell 2 like I am, this is the most useful information I've found:

    http://com2kid.wordpress.com/2011/09/09/how-to-properly-get-the-drive-letter-of-a-mounted-vhd-in-powershell/

    Hope this helps.

    Richard.

    • Proposed as answer by h3llr3is3r Saturday, January 17, 2015 10:48 PM
    • Unproposed as answer by h3llr3is3r Saturday, January 17, 2015 10:48 PM
    Thursday, December 20, 2012 11:17 AM
  • I have the same problem and it's a nuisance to have to write more PowerShell to search for the drive letter.  This example seems to work:

    select vdisk file=m:\test.vhdx

    attach vdisk

    select partition 1

    assign letter = R:

    I found that after "attach vdisk" the vdisk is now the default disk drive and as my disk only has one partition I selected that partition and assigned the drive letter I wanted.  When I had previously specified a different drive the system remembered it and "attach vdisk" mounted it automatically, but the "assign letter" then changed the drive letter to the new letter.

    • Proposed as answer by RJADuthie Tuesday, November 3, 2015 2:54 PM
    Sunday, September 1, 2013 1:13 AM
  • well that's kinda hard to automate in a batch script...
    Thursday, September 25, 2014 5:46 PM
  • i think what you looking for is a magic way. If you check the environemntal variable-s of the windows you will find that system drive is defined not by the letter, defined as %SystemDrive% or similar.

    I think this is what you looking for. As time passed by i think you figured out already.

    Saturday, January 17, 2015 10:50 PM
  • Suggestion.

    Create VHDX, attach it. Letter doesn't matter. Go to Storage Spaces and create one using that VHDX. You'll assign the letter you need in the storage space.

    Then you'll probably need just to attach the VHDX at startup.

    • Proposed as answer by Adskiy_sotona Friday, August 12, 2016 4:06 AM
    Friday, August 12, 2016 4:02 AM
  • Thanks for your suggestion, but I'm afraid you have missed the intent of my question by a considerable margin.

    Firstly, I asked for a solution that can be incorporated into a batch file. (A PowerShell script would be equally acceptable).

    Secondly, Storage Spaces is entirely irrelevant. That feature (along with VHDX support, for that matter) doesn't even exist in the specified OS (Hyper-V Server 2008 R2), and if it did exist, I don't know how I would "go to" it given that the OS has no GUI.

    Thirdly, this has nothing to do with attaching VHD's at startup.


    Friday, August 12, 2016 4:42 PM
  • Hey guys,

    Was in a similar situation where I was trying to assign a desired drive letter to a .vhdx file. In my specific case, I have my media files stored on a 200GB MicroSD card, in a .vhdx file (to avoid any restrictions/incompatibilities due to them being on a removable drive), and I wanted the system to automatically mount this "Media Libraries.vhdx" on system startup and assign it drive letter M.

    Spoiler: My solution for the drive letter changing part requires that you can manually interact with the VHD/VHDX file at least once before putting it through an autonomous script for the rest of its use.

    So, here's how I went about getting the drive letter changing to work in diskpart:

    1. In Windows Disk Management, attach the VHD/VHDX file (Action > Attach VHD)
    2. Add a directory mount path to the VHD/VHDX file (right-click on attached VHD > Change Drive Letter and Paths... > Add... > Mount in the following empty NTFS folder > eg. C:\MediaLibraryScript\MountPoint), this stays with the VHD/VHDX file through out its life, even after reboots, so long as the MountPoint directory exists.
    3. Now it is ready to be forever managed automatically via diskpart :) just use eg. select volume=C:\MediaLibraryScript\MountPoint to automatically select whatever volume # it got assigned, then eg. assign letter=m will always work.

    So, in my case, here's my fully functioning diskpart script that mounts the VHDX file to drive M (I run this on start up using Task Scheduler):

    select vdisk file="Z:\Media Libraries.vhdx"
    attach vdisk
    select volume=C:\MediaLibraryScript\MountPoint
    assign letter=m

    Happy new year ;)


    Static From MrElectrifyer


    Thursday, January 5, 2017 4:46 AM
  • All batch scripts I found in the internet which want to specify a fixed drive letter fail, here too.

    After so much years, there is no correct answer here. I found it, and updated this and similar questions on stackoverflow, etc. Solution was tested on several Windows versions from 7 to 10 and their respective server variants.

    The command 'diskpart' has a problem: After "attach vdisk" other commands are not executed as interactively, I guess, the command simply does not wait.

    Some answers got quite close to the solution, but they missed this main point.

    Anyway, to select a specific drive letter in a script, you have simply to call a diskpart command a second time. No problem because you are in a batch file or similar script. Excerpt:

    set diskPartScript=%TEMP%\%~n0.diskpart echo select vdisk file="%vhdPath%">"%diskPartScript%" echo attach vdisk>>"%diskPartScript%" diskpart /s "%diskPartScript%" del /q "%diskPartScript%" REM Assign the drive letter REM [it is assumed, that only 1 partition exists in the VHD]

    echo select vdisk file="%vhdPath%">"%diskPartScript%"

    echo select partition 1 >>"%diskPartScript%"

    echo assign letter=%driveLetter%>>"%diskPartScript%" diskpart /s "%diskPartScript%" del /q "%diskPartScript%"

    For the full batch script you could look here for example:

    https://superuser.com/questions/1022894/windows-7-command-script-to-mount-vhd-disk-with-assigned-drive-letter-via-diskpa/1199365#1199365





    • Edited by PhErdoes Thursday, April 13, 2017 4:37 PM
    • Proposed as answer by PhErdoes Thursday, April 13, 2017 4:37 PM
    Thursday, April 13, 2017 4:32 PM