Mounting a Virtual Hard Disk with Hyper-V
You can mount a virtual hard disk under the parent partition with Hyper-V and a simple script:
VBScript:
Option Explicit
Dim WMIService
Dim VHDService
Dim VHD
'Specify the VHD to be mounted
VHD = "F:\Windows.vhd"
'Get instance of 'virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")
'Get the MSVM_ImageManagementService
Set VHDService = WMIService.ExecQuery("SELECT * FROM Msvm_ImageManagementService").ItemIndex(0)
'Mount the VHD
VHDService.Mount(VHD)
PowerShell:
#Specify the VHD to be mounted
$VHDName = "F:\Windows.vhd"
#Get the MSVM_ImageManagementService
$VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "."
#Mount the VHD
$Result = $VHDService.Mount($VHDName)
This is a very straightforward script. You just get a MSVM_ImageManagementService object and call the "Mount" method on it. To unmount the virtual hard disk you run the same code but call "Unmount" instead.
Note that the virtual hard disk will be offline when it is mounted, so you will need to online the disk under the Server Manager after running this script.
Cheers,
Ben
Comments
Anonymous
February 05, 2008
You can also see some examples of Hyper-V WMI here: http://dungkhoang.spaces.live.com/default.aspxAnonymous
February 08, 2008
Of course, the vhd must not be in use by a guest at the time.Anonymous
February 24, 2008
Thanks for the article as I am just starting with Hyper-V and this really helped me out a lot. So how do you take your vbs script and make sure that the mounted VHD's are set online? That is the only problem I am running into. Thanks for any info.Anonymous
February 25, 2008
How to mount the VHD files on VM's using API's? (VB Script or Powershell)Anonymous
February 25, 2008
Hi, I've used the powershell script and got executed. I can see that the VHD file got locaked as well. Now, how do i view that VHD file from the parent OS? Thanks, HariAnonymous
August 21, 2010
The comment has been removedAnonymous
December 22, 2010
Ben, when I call Msvm_ImageManagementService::Mount(), in some rarely cases, it took a very long time. My code is waiting 20 minutes, but sometimes, after 20 minutes, it's state is still RUNNING (4) I dont't know what's the exactly reason, and want to know how to do in the followling: Continue to wait, or Unmount() it and try Mount() again. Any of your reply is appreciated. ThanksAnonymous
March 03, 2012
Thank you very much, Ben, this helped me and saved a lot of time and health : )Anonymous
April 11, 2013
Is this referring to older versions of Hyper-V (i.e. pre-Windows Server 2012)? On Windows Server 2012, I just go on the host system to the location where the VHD file is stored, right-click it and select 'Mount'. Much quicker and easier than any script. But I'm guessing you gave us the script because at the time you posted this capability didn't exist?Anonymous
November 12, 2013
Thank you! used the powershell script on a Virtual server with bluescreenofdeath.