| Profiel van Dung K HoangDung's spaceWeblogNetwerk | Help |
|
|
27 januari Hyper-V WMI Examples - Part I
Microsoft announced last week the Hyper-V WMI provider and published its documentation here. I find it very interesting for IT Pros like us to build scripts to manage and provision virtual machines through WMI. When combining PowerShell with WMI, you really have powerful tools in your hands for managing virtual environments. Let's take a look at the Hyper-V WMI provider through some examples. First you need a minimal environment to work with :=).
Note: You don't need to install any OS for guest systems, the examples below require some guests running ( even with no OS) to play with WMI. Here is a screenshot of my lab environment
Exploring the namespaceAll the examples below use Windows PowerShell as scripting language. PowerShell is the next generation of interactive shell from Microsoft. It's very powerful as it is an object-based shell, meaning that all input/output from PowerShell are objects and not text. IT administrators can leverage all objects model (WMI-COM-.NET) exposed by the OS and use them with PowerShell. Today, Windows PowerShell is shipped with Windows Server 2008 and there is a release of PowerShell on the Microsoft web site. Go and take a look at it here: www.microsoft.com/PowerShell Enough marketing for PowerShell =) Let's do some work now! First, open a Windows PowerShell window. PS C:\> get-wmiobject -namespace root -class __NAMESPACE | select Name Note: There are two(2) underscore (_) characters before NAMESPACE So the namespace for the Hyper-V WMI provider is root\virtualization. So far so good! Let's find all the classes exposed through this WMI provider PS C:\> get-wmiobject -namespace root\virtualization -list This command returns all classes that exist under this namespace and you will notice that there are hundreds of them! Their names start with CIM_ or Msvm_. CIM classes provide information about system in general (based on DMTF standard) and Msvm classes are more specific to Windows Virtualization environment.
The Msvm_ComputerSystem ClassAs its name implies, the Msvm_ComputerSystem class contains information about a system which is either a host system or a virtual system (or virtual machine -VM). Use this class as follow: PS C:\> get-wmiobject -namespace root\virtualization -class Msvm_ComputerSystem This command returns all instances of Msvm_ComputerSystem, each object represents a virtual system or a host system. The output produces a lot of information as the class has several properties. By the way if you want to find out all properties of the class, simply pipe the previous command to get-member. Find properties that are of your interest and filter the output of the previous command to find useful information. In the following example, I will focus on those properties: ElementName , Name and Caption or Description. The ElementName attribute is set to the display name of a virtual machine or the NETBIOS name of the host system. The Name attribute contains a GUID of the computer object if the computer is a virtual machine. When the Name contains a “real” name, the corresponding object describes a host system. The Description or Caption attribute can also give some hint about the type of the computer. In my environment, the output of this command shows that the host system is named VIRIDIAN and is hosting 4 virtual systems To find out all virtual systems on a given host, you can filter the output of the previous command as follow: PS C:\> get-wmiobject -namespace root\virtualization -class Msvm_ComputerSystem -filter " ElementName <> Name" Another interesting property is the EnabledState property. According to the documentation here, it indicates the current state of a system. It is always set to 2 ( Enabled) for a host system but for virtual systems, it can take one of the following values: 2 - Enabled ; 3- Disabled; Paused - 32768 ; Suspended - 32769 Values in the range [32770-32775] indicate transitional states of a system: Starting, Suspending, Saving.... Now that you discover, how can you change the state of a given machine? Well, the Msvm_ComputerSystem class has a method called RequestStateChange that you can use to perform actions such as, turn off, pause or save state of a virtual system. Its first parameter is of type Integer and can take on of the values described above. Here is an example: First find a virtual machine to work with Then check is current state Put this machine in saved state Start this machine Isn't it easy? SummaryIn this blog entry, we discover the namespace of the Hyper-V WMI provider and use get-wmiobject to find all the classes exposed through this provider. Hope you enjoy it! Until the next time.... /Dung |
|
|