Hyper-V WMI Using PowerShell Scripts – Part 2
In part 1 we went over some basic scripts and tools for gathering information about running virtual machines. In part 2 I am going to cover two things, first basic VHD creation and second determining if Hyper-V WMI methods are succeeding or failing and what the error message is.
PowerShellPlus
Last time I showed the PowerShell 2.0CTP which includes the Graphical PowerShell interface… Those of you that plan to do a lot PowerShell development might want to check out PowerShellPlus (http://www.powershell.com/index.html). It’s a $79 investment if you are using it for commercial use, but there is a 30 day trial – 1 day down and so far I think it’s worth $79… As you can see from the screen capture below the editor offers auto-complete as well as a pretty good debugger.
Creating a VHD Using PowerShell WMI
Creating a VHD using the Hyper-V WMI is pretty easy… And once again PowerShell makes it even easier…
The first command retrieves a WMI object for Msvm_ImageManagmentService.
The second command executes the CreateDynamicVirtualHardDisk method to create a new dynamic VHD. PowerShell is smart enough to know that 20GB = 20 * 1073741824... That’s so COOL!.
|
PS D:\> $VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" PS D:\> $VHDService.CreateDynamicVirtualHardDisk("D:\vhds\TestVhd1.vhd", 20GB)
__GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 2 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : Job : \\TAYLORB-DP490\root\virtualization:Msvm_StorageJob.InstanceID='Microsoft:Msvm_{B5A1A0EC-6A86-4CB0-AF7A-B336CBA2DCFF}' ReturnValue : 4096 |
Finding Out If Your VHD Creation Succeeded Or Not…
Let’s say you actually want to know if the VHD was created successfully or not (crazy I know…). Well normally you would just check ReturnValue, but what the heck does 4096 mean? 4096 means that the method is executing asynchronously (in the background) and you need to check it’s Job object to see when it’s finished and if it was successful…
As you can see from the ErrorDescription below my job actually failed since I didn’t delete the VHD between the first and second sample… Most of the Hyper-V WMI uses these Job objects so this is a pretty handy bit of code to know…
Again the first command just retrieves a WMI object for Msvm_ImageManagmentService.
The second command is mostly the same except for three key parts, first you can see it stores the return object in $Job but the real magic is the [WMI] before the $VHDSer… What’s happening is your telling PowerShell to make you a PowerShell WMI object for the Job path stored in the Job field. Look at the output from the sample above the Job field is a path to a WMI job object...
The third command is just showing the output of the $Job… You could do $Job.ErrorDesciption if you wanted to just get the ErrorDescription..
|
PS D:\> $VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" PS D:\> $Job = [WMI]$VHDService.CreateDynamicVirtualHardDisk("D:\Users\Public\Documents\Hyper-V\Virtual hard disks\TestVhd1.vhd", 20GB).Job PS D:\> $Job
__GENUS : 2 __CLASS : Msvm_StorageJob __SUPERCLASS : CIM_ConcreteJob __DYNASTY : CIM_ManagedElement __RELPATH : Msvm_StorageJob.InstanceID="Microsoft:Msvm_{22FE6994-4CD8-4D06-9F3D-906A23A4BB09}" __PROPERTY_COUNT : 43 __DERIVATION : {CIM_ConcreteJob, CIM_Job, CIM_LogicalElement, CIM_ManagedSystemElement...} __SERVER : TAYLORB-DP490 __NAMESPACE : root\virtualization __PATH : \\TAYLORB-DP490\root\virtualization:Msvm_StorageJob.InstanceID="Microsoft:Msvm_{22FE6994-4CD8-4D06-9F3D-906A23A4B B09}" Caption : Storage Job Child : DeleteOnCompletion : True Description : Disk Creation Job. ElapsedTime : 00000000000000.000000:000 ElementName : Storage Job ErrorCode : 32768 ErrorDescription : The system failed to create 'D:\Users\Public\Documents\Hyper-V\Virtual hard disks\TestVhd1.vhd' with error 'The f ile exists..' (0x80070050) ErrorSummaryDescription : HealthState : 5 InstallDate : 00000000000000.000000+000 InstanceID : Microsoft:Msvm_{22FE6994-4CD8-4D06-9F3D-906A23A4BB09} JobCompletionStatusCode : 2147942480 JobRunTimes : 1 JobState : 10 JobStatus : Error LocalOrUtcTime : 2 Lun : 0 Name : Msvm_StorageJob Notify : OperationalStatus : {2} OtherRecoveryAction : Owner : Parent : PathId : 0 PercentComplete : 0 PortNumber : 0 Priority : 0 RecoveryAction : 3 RunDay : RunDayOfWeek : RunMonth : RunStartInterval : ScheduledStartTime : StartTime : Status : Error StatusDescriptions : {Error} TargetId : 0 TimeBeforeRemoval : 00000000000000.000000:000 TimeOfLastStateChange : 00000000000000.000000+000 TimeSubmitted : Type : 1 UntilTime :
|
Well I think that's going to have to do it for tonight… Stay tuned I have A LOT more to show.
If you prefer the examples in this format tell me, if you prefer the format from yesterday tell me, if you want a different format altogether tell me…
-Good Night and Good Weekend
-Taylor Brown
I have been working on virtualization solutions at Microsoft for over 4 years, I started on the team shortly after Microsoft acquired Connectix in 2003. I worked on Virtual Server where I tested a verity of things but focused on networking, serial, and power management. I currently lead the Hyper-V End-to-End team, we are responsible for ensuring the end to end functionality of Hyper-V, this includes working with customers/partners/oem’s/etc… to ensure we are testing and shipping the best quality software possible. I have also worked on the Microsoft 1394 (firewire) team, ACPI (power management) team, and did a brief stent on the Microsoft Smart Watch team as a contractor.