Thursday, April 16, 2009

Creating Functions in PowerShell

I have told you before in my previous blogs about Hyper-V and PowerShell to play with VHDs using WMI

This requires that every time we need to get VHD info we have to write  the below

$computer = "LocalHost"
$namespace = "root\virtualization"
$disk=Get-WmiObject -class Msvm_ImageManagementService -computername $computer -namespace $namespace

then we have to remember the method name or use get-member commandlet then we can use it like below:

$disk.GetVirtualHardDiskInfo(“c:\vhds\sccom.vhd”)

What I will add today that we can create something like command-let; this is so called functions in PowerShell

It is very easy just type the function name followed by open brackets { then   type your commands in one or more lines then close it by }

Lets create function to give us VHD disk info and lets name it get-diskinfo

function get-diskinfo {
$computer = "LocalHost"
$namespace = "root\virtualization"
$disk=Get-WmiObject -class Msvm_ImageManagementService -computername $computer -namespace $namespace
$disk.GetVirtualHardDiskInfo(“c:\vhds\scom.vhd”)
}

then you just execute the function name like get-diskinfo then it will work seamlessly

image

the problem that we will execute it and every time it will execute against the same disk

we need to use variables or we can name it parameters.

There are variables called $args[0] or $args[1] or $args[2] and so one; these can be used to get parameters when you execute the command so $args[0] is the first parameter then $args[1] is the second parameter and so on.

Then we will add $args[0] after the $disk.GetVirtualHardDiskInfo( like $disk.GetVirtualHardDiskInfo($args[0])

function get-diskinfo {
$computer = "LocalHost"
$namespace = "root\virtualization"
$disk=Get-WmiObject -class Msvm_ImageManagementService -computername $computer -namespace $namespace
$disk.GetVirtualHardDiskInfo($args[0])
}

so we can now type 

get-diskinfo “c:\vhds\scom.vhd”

the final question running into my head; when we close the PowerShell console this function will remain or it will disappear

actually it will disappear so we have to load it everytime the console starts

to do so we have something called profile script which automatically starts every time you run the PowerShell console

to access the your console profile script you can type

notepad $profile

it will open the profile in the notepad almost it will give you error as the file is not created yet

so you can use command like

fsutil file createnew $profile 1

This will create file on this path %Userprofile%\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Now, you can edit it and add your functions so you can use it everytime you lunch the PowerShell Console.

image

Thank you for reading and I hope this was usefull.

Monday, April 13, 2009

Reconnect VHD Parent Disk

Dear my readers,

As stated in a previous blog, there is three main types of the disks.

the Child and parent disks where you can have some sort of base disk contains all sysprepped image of the windows and another child disk contains the currently running system like base for windows 2003 sp2 and multiple child disks contains different applications

anyway what I am trying to address is when you move your parent files to another directory and try to open the child VHDs in your VMs you will get a message in Virtual PC saying please locate your Parent disks, but if you are using HyperV you will get error message which says you cannot start the machine due to disk failure….

You can use the WMI to connect back your Child disk with the parent in the new location

we will declare variable for the wmi class we working with.

$vhd=gwmi Msvm_ImageManagementService –namespace root\virtualization –comp $computer

we will define the disks as variables

$child=c:\vhds\child.vhd

$parent=c:\vhds\parent.vhd

we will define the computer we are trying to connect to in the $computer variable; if it localhost we will use “.” dot if it is remote we will use the accessible hostname or netbios name.

$computer=.

now it is ready to use our method ReconnectParentVirtualHardDisk which help us the solve the problem stated above, as we learnt before that if we want to know how to use certain method then we use get-member and look for the syntax in the definition

$vhd |gm ReconnectParentVirtualHardDisk |fl

TypeName   : System.Management.ManagementObject#root\virtualization\Msvm_ImageManagementService
Name       : ReconnectParentVirtualHardDisk
MemberType : Method
Definition : System.Management.ManagementBaseObject ReconnectParentVirtualHardDisk(System.String ChildPath, System.String ParentPath, System.Boolean Force)

so now we will connect the child to new parent.

$vhd.ReconnectParentVirtualHardDisk($child,$parent,1)

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

Monday, April 6, 2009

How to Mount Disks to your physical Machine using Powershell

1st you have to have HyperV Role installed

define the ImageManagementService Class as Variable

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

Call mount method which require only the full path for your vhd drive like the example below:

$disk.mount("C:\Program Files\Microsoft Learning\50028B\Drives\50028-DC1-Data.vhd")

once the command succeed you will see like new drive is being installed in your notification area

image

the problem now that the disk is currently offline like the disk 2 in the image below so we have to bring online and then assign Drive letter.

image

we can do this also by Powershell but it is very long I will add it later.

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)

Saturday, April 4, 2009

Adding Microsoft Loopback Adapter in Windows 7 and Windows 2008 R2

I have tried to install Ms Loopback adapter to Windows 2008 R2

I used the normal method which is open Control Panel and select Add a device link and actually it did not list the device like previous versions.

I found the solution is very easy all you have to do is to launch the old add hardware wizard hdwwiz.exe and do the same steps like the previous versions.

Start by press Start button or Windows button depend on your GUI then type hdwwiz.exe then right click it and choose run as Administrator to ensure that the process will succeed

image

Press next to continue to the next page

image

Select the the second option to ensure that you will be able to identify your card.

image

Select Network Adapters.

image

Select Microsoft from Manufactures list then select Microsoft Loopback Adapter

image

image

image

image

now you have an new adapter as loopback

image