Mounting a Virtual Hard Disk with Hyper-V

Mounting a Virtual Hard Disk with Hyper-V

Rate This
  • Comments 10

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

Leave a Comment
  • Please add 5 and 3 and type the answer here:
  • Post
  • You can also see some examples of Hyper-V WMI here:

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

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

  • 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.

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

  • 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

  • Thanks, great help it brought to my deployment. :)

    I've also tried Core Configurator coreconfig.codeplex.com

    on the Hyper-V R2 ... and it works like a charm,

    with the ability to monitor Your virtual machines locally

    (connected from Windows XP and Linux using RDP).

    Actually I've also managed to run SharpDevelop version 3 on Hyper-V R2

    - You ask what for ? To develop and compile some GUI managment extensions.

    - one gets warning about missing IE COM components, that SharpDevelop use

    at the startup to present some news, but after You click Continue,

    the IDE just working fine and You are able to compile any Windows Forms

    or console app.

    Before You run the SharpDevelop installer, You need to install NET Framework 3.0

    and perform Windows Update routine.

    I've just used ocsetup:

    start /w ocsetup NetFx3-ServerCore

    start /w ocsetup NetFx3-ServerCore-WOW64

    Actually not able to use IE, I downloaded and used alternative webbrowser.

    Also works.

  • 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.

    Thanks

  • Thank you very much, Ben, this helped me and saved a lot of time and health : )

  • 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?

  • Thank you! used the powershell script on a Virtual server with bluescreenofdeath.

Page 1 of 1 (10 items)