Feature of the Week: Windows Server 2008 Hyper-V Scripting
Virtualization is on everybody's mind — and with good reason. It's a critical, sea-changing concept with wide-reaching implications. The idea that you can make pools of dynamic resources with unlimited capacity available to users anywhere at any time is extraordinary.
Because it is so vital, Microsoft® is committed to driving the adoption of virtualization. However, unlike today's conventional wisdom, we don't view virtualization as an isolated, tactical tool. Rather, since virtualization can have a profound impact on your entire operation from the datacenter to the desktop, Microsoft believes it should be embraced as part of an enterprise-wide infrastructure strategy.
Windows® Management Instrumentation or WMI is the essential underlying technology for managing Windows Server 2008 Hyper-V. WMI allows administrators and IT professionals full access to the Hyper-V stack.
Virtualization WMI Classes
Hyper-V does not expose a COM API for scripted management like previous products from Microsoft. Instead, Hyper-V is managed by working with WMI classes. Make sure to browse the MSDN section on the virtualization WMI classes to become familiar with the different classes and their properties.
For today’s Feature Of The Week we will use VBScript and PowerShell. In order to write VBScript code, you may use PrimalScript by SAPIEN Technologies or your preferred editor Notepad. To write Windows PowerShell scriptlets, you must have at least Windows PowerShell 1.0 installed. Windows PowerShell is an optional component for Windows Server 2008 and can be installed by using the "Add a Feature" option in Server Manager. Finally, for the VB.NET and Visual C# code snippets, you can use Visual Studio Express.
Ok, assuming you have these tools installed, let’s go ahead and start scripting!
Scripting Hyper-V with VBScript
In the examples we are going to expose, we will be doing very simple Hyper-V WMI querying. In this case, the script we want to write will list all of the Virtual Machines (VMs) running in a local Windows Hyper-V installation (you can find this script in the code snippets included with this newsletter).
Here’s how you find the information using VBScript:
The first line of the script sets the variable strComputer to “.” which is used to specify the name of the server to query the WMI service (in this case, the local server). If you wanted to connect to a remote instance, then you would only need to change this to the name of the remote machine.
The second line obtains the WMI service object from the virtualization namespace. The next section gets the name of the host on which this script is running, then queries for the Msvm_ComputerSystem object that represents the host. Since ExecQuery() returns a collection, it is necessary to get the single object in the collection using a For Each loop.
After we have the host, we query for all Msvm_ComputerSystem objects associated with that host via Msvm_HostedDependency objects. These objects represent all the VMs running on that host. Finally, the foreach statement is responsible for displaying the ElementName and Caption properties of each virtual machine. Pretty straightforward, don’t you think?
Scripting Hyper-V with PowerShell
PowerShell has to be, without a doubt, one of the most useful — if not the most useful — scripting technologies on the Windows platform. Not only does it allow you to write powerful scripts but it also allows you to utilize .NET objects —something that VBScript cannot do.
One of the key features of PowerShell is how easy it is to understand. It follows noun-verb syntax, and that makes the comprehension of scripts very straightforward.
Let’s get back to our original example. If you recall, we wanted the script to output the names of the virtual machines with their installation date. The script to do this in PowerShell follows:
Just like in our previous example, the first line uses the Get-WMIObject (which can be abbreviated as gwmi) command to obtain the WMI object for the host in the virtualization namespace. A list of all Msvm_ComputerSystems is piped over to the next command. The where clause is responsible for finding the parent partition — this is analogous to the if statement we introduced in the VBScript. Once again, we make a second WMI query to find all VMs hosted on this machine. Finally, the last line is used to display the elements ElementName and Caption. An example on what the output looks like in our test machine follows:
You will find that this is a lot easier than the VBScript solution. Reading the script and understanding is also easier than the VBScript solution; you could probably understand what the script is doing if you had never read the description on what it does.
That’s it of this week’s FoW on Hyper-V scripting.