Freigeben über


Scripting: Hyper-V - Mount VHDs by right clicking

There are plenty of posts on the interweb that show you how to mount and unmount vhds via powershell.  I downloaded the Hyper-V PowerShell management library from CodePlex.com here as created by James O'Neil.  In it he kindly provides two scripts (mount-VHD.ps1 and Unmount-VHD.ps1) along with a REG file.  Assuming you have PowerShell 1.0 installed (available feature in Windows Server 2008) these scripts and registry settings work fine. 

I ran into problems once I downloaded and installed the Windows PowerShell 2.0 Community Technology Preview (CTP).  Powershells execution policy wouldnt let the scripts run anymore.

You can change the executionPolicy a number of ways:

Registry:  

HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell

Change the key: REG_SZ ExecutionPolicy to Unrestricted

PowerShell:

set-executionpolicy unrestricted

Note: By changing the execution policy you are technically opening your system up to remote execution of PowerShell scripts from unsigned/untrusted sources.  I want to be able to mount vhds easily coz Im a lazy kinda guy.   Im running Hyper-V on my laptop so Im not too concerned about security in this instance.  You should think carefully about making this change in a production environment.

The second thing I noticed was that the registry settings provided by James no longer worked.  So I came up with a slight modification as follows:

 Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Virtual.Machine.HD]

[HKEY_CLASSES_ROOT\Virtual.Machine.HD\DefaultIcon]
@="%SystemRoot%\\system32\\imageres.dll,26"

[HKEY_CLASSES_ROOT\Virtual.Machine.HD\shell]
@="Mount"

[HKEY_CLASSES_ROOT\Virtual.Machine.HD\shell\Mount]

[HKEY_CLASSES_ROOT\Virtual.Machine.HD\shell\Mount\command]
@="cmd /k \"powershell -NoProfile -Command \"& 'c:\\Program Files\\Hyper-V\\Mount-VHD.ps1' '%1'\"\""

[HKEY_CLASSES_ROOT\Virtual.Machine.HD\shell\Unmount]

[HKEY_CLASSES_ROOT\Virtual.Machine.HD\shell\Unmount\command]
@="cmd /k \"powershell -NoProfile -Command \"& 'c:\\Program Files\\Hyper-V\\Unmount-VHD.ps1' '%1'\"\""

[HKEY_CLASSES_ROOT\.vhd]
@="Virtual.Machine.HD"

I've used cmd/k instead of cmd/c so I can see what the PowerShell script reports when its finished along with a couple of changes to get PowerShell to accept the string after the -Command.

Now all is great in the land of Hyper-V on my laptop.

Comments

  • Anonymous
    January 01, 2003
    There are plenty of articles out there explaining how to install the Hyper-V and get a Windows Server