Hyper-V WMI Using PowerShell Scripts – Part 1 (Worker Process ID to Virtual Machine Mapping)

Hello out there in blog land… This is Taylor Brown some of you may know me from the Virtualization Deployment Summit or from my posts on the TechNet Forums, for those who don’t know me I am a test lead on the Hyper-V team. One of my team’s responsibilities is validating end-to-end integration of all the different parts of Hyper-V such as networking, storage, user-interface or installation. We are also responsible for the relationship between you the customer and the rest of the Hyper-V test team. 

Last week I did a demo of some Hyper-V WMI scripts in Powershell and enough people asked for them I figured I'll just post them for everyone… So without further delay here's part 1.

PowerShell 2.0 CTP – Graphical PowerShell Interface

I highly recommend the PowerShell Graphical Interface that is included in the PowerShell 2.0 CTP. 

You can read more about it and download the installation package at https://www.microsoft.com/technet/scriptcenter/topics/msh/download2.mspx .

It offers a nice tabbed interface and color coding, its a CTP build so it’s far from bug-free but the benefits out way the pitfalls. You have to uninstall the PowerShell 1.0 Windows update before you can install the CTP build. I don't think I would install the CTP on your production servers, but the PowerShell scripts are generally compatible.

The biggest downside to this interface for me is that it makes me want more features like auto-complete.

PowerShell20CTP

 

Mapping Worker Process ID's to Virtual Machines

Each virtual machine has it's own worker process (vmwp.exe) instance when it's running. Some people have asked how they can map worker process to a specific virtual machine so here's the answer... This is also a great example of the power that WMI and PowerShell have with Hyper-V. 

    1: $Vm = Get-WmiObject -Namespace root\virtualization -ComputerName "." `
    2: -Query "Select * From Msvm_ComputerSystem Where ElementName='Server 2008 - Test1'"
    3: $Vm.ProcessID
    4:  
    5: $Vm = Get-WmiObject -Namespace root\virtualization -ComputerName "." `
    6:    -Query "Select * From Msvm_ComputerSystem Where ProcessID=4044"
    7: $Vm.ElementName

Lines 1 and 2 retrieves a WMI object for the virtual machine with the friendly name of 'Server 2008 - Test1'. Then line 4 outputs the value in the ProcessID field to the console.

Lines 5 and 6 retrieves the same WMI object as lines 1 and 2 did but retrieves it via the process id. Then line 7 outputs the value in the ElementName (friendly name) of the virtual machine to the console.

Other WMI Properties Of the Msvm_ComputerSystem

msvm_computersystem

One of the best parts about writing scripts in PowerShell over VBScript or JScript is that like a command or batch script you can run commands in the shell window and see the results right away...

In this screen capture I just ran the command from line 1 and 2 above (hint remove the ` and carriage return from the end of line 1 to make the command a single line).

Then I ran $Vm, which is the WMI object I retrieved. This will output all of the properties that object contains. Here's the list - stay tuned for later posts and I will talk about what these properties mean.

PS C:\> $Vm = Get-WmiObject -Namespace root\virtualization -ComputerName "." -Query "Select * From Msvm_ComputerSystem Where ElementName='Server 2008 - Test1'"
PS C:\> $Vm

__GENUS : 2
__CLASS : Msvm_ComputerSystem
__SUPERCLASS : CIM_ComputerSystem
__DYNASTY : CIM_ManagedElement
__RELPATH : Msvm_ComputerSystem.CreationClassName="Msvm_ComputerSystem",Name="A1223A4B-887D-4F01-895E-FBFB636F76D8"
__PROPERTY_COUNT : 29
__DERIVATION : {CIM_ComputerSystem, CIM_System, CIM_EnabledLogicalElement, CIM_LogicalElement...}
__SERVER : TAYLORB-DP490
__NAMESPACE : root\virtualization
__PATH : \\localhost\root\virtualization:Msvm_ComputerSystem.CreationClassName="Msvm_ComputerSystem",Name="A1223A4B-887D-4F01-895E-FBFB636F76D8"

AssignedNumaNodeList : {0}
Caption : Virtual Machine
CreationClassName : Msvm_ComputerSystem
Dedicated :
Description : Microsoft Virtual Machine
ElementName : Server 2008 - Test1
EnabledDefault : 2
EnabledState : 2
HealthState : 5
IdentifyingDescriptions :
InstallDate : 20080502051703.000000-000
Name : A1223A4B-887D-4F01-895E-FBFB636F76D8
NameFormat :
OnTimeInMilliseconds : 3783756
OperationalStatus : {2}
OtherDedicatedDescriptions :
OtherEnabledState :
OtherIdentifyingInfo :
PowerManagementCapabilities :
PrimaryOwnerContact :
PrimaryOwnerName :
ProcessID : 4044
RequestedState : 12
ResetCapability : 1
Roles :
Status :
StatusDescriptions :
TimeOfLastConfigurationChange : 20080502051707.000000-000
TimeOfLastStateChange : 20080502051707.000000-000