Monday, April 6, 2009

Creating Dynamically Expanded VHD using Powershell

simply you have to have Hyper-V Role installed on the machine where the task will be executed.

start your Powershell Console

set variable as the class we would like to use like the below

$var = commandlet

and as we will use WMI class to deal with the VHD we have to use command called get-wmiobject this allow us to use the Msvm_ImageManagementService from the Virtualization namespace which is located under root\Virtualization and on the local computer by using –computer . or we can replace the . with remote computer name

Get-WmiObject Msvm_ImageManagementService -namespace "root\virtualization" -computername .

so we need to use method named CreateDynamicVirtualHardDisk in Msvm_ImageManagementService class to create our dynamic disk.

Note: to get the methods you can pipeline the command above with get-member command like in the picture below.

image

so to get back on how to execute a method we have to insert our WMI Class in variable by typing

$disk = Get-WmiObject Msvm_ImageManagementService -namespace "root\virtualization" -computername .

then to call the method by typing $var.method()

$disk.CreateDynamicVirtualHardDisk("C:\dynamic.vhd", 20000000000)

and as this method requires two input 1st is the VHD path location and the second is the size in bytes

as in the example the 20000000000 will create 20 GB

now the command should be executed and display

image

if you go and inspect the file using your hyper-v

image

so we have two lines to create the VHD disk

$disk = Get-WmiObject Msvm_ImageManagementService -namespace "root\virtualization" -computername .

$disk.CreateDynamicVirtualHardDisk("C:\dynamic.vhd", 20000000000)