Virtual PC Guy's Blog

-- Ben Armstrong, Virtualization Program Manager

Talking about core virtualization at Microsoft (Hyper-V, Virtual PC and Virtual Server).

Welcome to MSDN Blogs Sign in | Join | Help

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

Published Friday, February 01, 2008 9:55 PM by Virtual PC Guy

Comments

# re: Mounting a Virtual Hard Disk with Hyper-V

You can also see some examples of Hyper-V WMI here:

http://dungkhoang.spaces.live.com/default.aspx

Tuesday, February 05, 2008 8:42 AM by Dung K Hoang

# re: Mounting a Virtual Hard Disk with Hyper-V

Of course, the vhd must not be in use by a guest at the time.

Friday, February 08, 2008 5:55 PM by stephc

# re: Mounting a Virtual Hard Disk with Hyper-V

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.

Sunday, February 24, 2008 11:40 PM by Jason

# re: Mounting a Virtual Hard Disk with Hyper-V

How to mount the VHD files on VM's using API's? (VB Script or Powershell)

Monday, February 25, 2008 10:43 PM by Josh

# re: Mounting a Virtual Hard Disk with Hyper-V

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,

Hari

Tuesday, February 26, 2008 6:30 AM by Hari
New Comments to this post are disabled
 
Page view tracker