Login with Facebook

Thursday, April 9, 2009

Creating Differencing VHD using Powershell; read it to the end

Dear my blog-spectateur,

I just would like to highlight, I am not writing this to tell you another way to create another type of VHDs but I am creating this to let you know how to get different methods out of the WMI

Basically as you may know there are different types of VHDs in Microsoft implementation of virtual hard disk

First there is the fixed size where it takes the same amount of physical disk and represent it in your Virtual Machine

2nd they dynamic disk or what they call it dynamically expanding disk where it takes a tinny amount of Disk space just to identify disk properties like the maximum size and then expands by adding additional data to the file.

The 3rd type is the differencing disk where you have a disk either dynamic or fixed used as a parent and another VHD dynamically expands as long as you add additional data to the disk and the parent is used in the read operations. best example like to need to test something to your VM so you can create difference of your parent and then power on the VM using the difference then what ever you add to your machine it will not affect your parent disk.

I have demonstrated in a previous blog how you can create your dynamically expanding disk.

What I am going to do now is to use the Powershell along with the WMI to create Difference disk using the same like the previous blog

We will search the WMI class for a method to create the dynamic disk we can use a Powershell Command-let called get-member which helps you to get the properties and methods of objects.

so lets try the command now

image

as you see above, it lists all the available data that you can get from this class it list the methods which are commands you can initiate and properties which are the attributes about this class and its instances like caption where is the name of the class.

So we can list the class attributes by just type the command-let without the pipelining

image

this command output is formatted as a list “line per attribute” the way this output is displayed is the the default of the Powershell is to display output using a command format-list or its alias FL

so the command is completed as:

PS C:\> Get-WmiObject Msvm_ImageManagementService - namespace "root\virtualization" -computername . format-list

but if you need to get spacific attributes “properties” you can type format-list -property Caption, InstallDate or you can type the properties directly without typing -property like this

Get-WmiObject Msvm_ImageManagementService -namespace "root\virtualization" -computername . format-list Caption, InstallDate

image

So, I assume we know how to use the properties and how to list it just give a try and use format-wide and format-table instead of format-list.

Lets move to the methods

Methods are are like commandlets available in this class like in here there are a list of methods and we can use the command

get-member –membertype method

image

So, as per the blog topic we need to create Difference disk so we have a method called CreateDifferencingVirtualHardDisk where we will use but the problem is how we should use it, what is the appropriate format.

We will use the command get-member to show only the method we need then we will pipeline it to format-list to display the information clearly

image

In the highlighted section which is Definition you will see the the function, oops I mean the method requires two parameters (1st one is the vhd drive path , then the parent VHD path)

So, lets apply what we have learnt now:

I have a parent disk named W2k3sp2base32.vhd and I would like to create a disk name scomr2.vhd both files are located under C:\Vhds\

So, first to use the method I have to add the class to variable

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

Then I will call the method from the variable and i will use () included with the parameters needed.

$vhd.CreateDifferencingVirtualHardDisk(“c:\VHDs\scomR2.vhd”, “c:\VHDs\W2k3sp2base32.vhd”)

image

Look at the returned value is 4096 which means Method Parameters Checked – JobStarted. which hopefully means Ok

you can get all the values from http://msdn.microsoft.com/en-us/library/cc136778(VS.85).aspx

I hope this was useful and I hope to get your feed back…

Mahmoud Ibrahim
Senior Consultant Infrastructure Solutions
OMS ME

OMS

No comments:

Post a Comment