Okay! Now that initial documentation of the Hyper-V WMI API is available I thought I would respond with a "Week of Hyper-V Scripts".
Starting with a simple one – here is a script that will list the name, identifier and state for each virtual machine on the physical computer:
Now to pull these scripts apart a bit:
- The flow of these scripts is:
- Get WMI Service object for virtualization namespace
- Execute WMI query to get all VM objects
- Iterate over the VM objects
- For each virtual machine object we display the "ElementName" – which is the friendly name that you give the virtual machine ("Windows Server Foo") – the "Name" – which is a GUID that is used to internally uniquely identify the virtual machine and the "EnabledState" (you can find what the different EnabledState values mean here: http://msdn2.microsoft.com/en-us/library/cc136822(VS.85).aspx).
- "gwmi" is PowerShell shorthand for "Get-WMIObject"
- Amusingly enough this script would also return information about the parent partition (which is technically a virtual machine) which is why I check the caption of the virtual machine and only display information about entries that are actually virtual machines.