This article was first published on SearchServerVirtualization.TechTarget.com.

Series Introduction

Among the many benefits of deploying virtualization technology is the ability to better manage complex environments. When implemented correctly, it’s often easier to manage virtual machines than it is to keep track of physical ones. But what happens when you end up with a large number of VMs. Organizations that start by dabbling in virtualization often find that the number of virtualization hosts and guest OS’s can quickly get out-of-hand. Add to that the fact that virtualization management tools are still evolving, and you can end up with quite a challenge for systems administrators.

When dealing with physical servers, many IT pros and end-users have found ways to use automated methods of handling otherwise tedious tasks. For example, performing backups and deploying operating system updates are generally done manually in only the smallest of environments.

When it comes managing Microsoft Virtual Server VMs, there’s a readily available method for automation. In this series of articles, Automating Virtual Server, I’ll present details and examples of how you can programmatically manage your VMs. Code samples will be provided in VBScript, Visual Basic .NET, and C# syntax. Some experience with scripting and application development is assumed, but I’ll provide links to relevant information if you need to get up to speed. Overall, I think you’ll find that the process is fairly easy and straight-forward.

Automating Virtualization

If you’re a systems administrator or developer that’s faced with managing a large number of virtual machines, there’s a good chance that you find yourself wishing for a robot that could handle some of the more mundane tasks. While building such a device is more than could be covered in a single article, the focus here is to demonstrate how you can create scripts for performing routine tasks in Virtual Server. We’ll begin by using the VBScript language in this article, since it’s the easiest way to get up and running. In later articles, I’ll provide code samples in .NET languages (C# and Visual Basic.NET).

Virtual Server’s COM API

One of the built-in features of Microsoft Virtual Server is a fully-documented Component Object Model (COM) API. By connecting to this API, you can perform pretty much any task that can be carried out manually using Virtual Server’s built-in tools. In fact, the Virtual Server Administration Web Site itself is simply a web-based user interface that connects to the COM API behind the scenes.

The basic structure of the COM API is based on collections. Figure 1 provides an overview of the some of the most commonly-used ones. The figure is not a complete object model, and the full details can be found in the Virtual Server Programmer’s Guide (a help file that’s automatically installed with the product). The Programmer’s Guide includes details about all of the objects that are available, along with a list of properties, methods and enumeration constants. If you’re doing more than basic scripting, you’ll probably find yourself referring to that file often.

image

Figure 1: An overview of the Virtual Server COM object model

Getting Started with VBScript

It’s beyond the scope of this article to cover the details of the VBScript language. If you’re not familiar with it, some good starting points for more information are the Microsoft Developer Network (MSDN) Scripting site (http://msdn.microsoft.com/scripting) and the Microsoft TechNet Script Center (http://www.microsoft.com/technet/scriptcenter).

VBScript is based on the Visual Basic language syntax, and it’s a very simple language for getting started with automation. The language is not case-sensitive and it provides for automatic data type conversions. These are two “features” that can help get you up and running quickly (though they’re not good practices when writing production applications).

Another advantage of writing VBScript code is that there’s no requirement for a development environment: Just use your favorite text editor. In most cases, you can copy and paste the code in this article and use it directly on a Windows computer that’s running Virtual Server. OK, let’s get started.

Connecting to Virtual Server

Generally, the first programmatic step you’ll take when creating a new VBScript file for automating Virtual Server is to create a new object that attaches to the COM API. The following statement takes care of this:

Set objVirtualServer = CreateObject(“VirtualServer.Application”)

As long as the Virtual Server product is installed on the local computer, this script should run as-is. By default, this will connect to the instance of Virtual Server that is running on the local machine. If you want to connect to an instance of Virtual Server that’s running on a remote computer, you can use a link such as the following:

Set objVirtualServer = CreateObject(“VirtualServer.Application”,”RemoteServerName”)

Note that you’ll need to have the appropriate security settings and configuration to be able to remotely connect to a COM object (you can get details on these topics using the links provided earlier).

Let’s prove that everything’s working by adding the following two lines to get some information about the local Virtual Server instance:

WScript.Echo “Server Name: “ & objVirtualServer.Name

WScript.Echo “Server Uptime (sec): “ & objVirtualServer.Uptime

Now, the script will output the name of the instance (which should be the same as the machine name), along with the number of seconds that the Virtual Server service has been running. We did this by accessing properties of our “objVirtualServer” object. You can use other methods and properties of the Virtual Server object to perform tasks such as programmatically creating virtual hard disks and changing server settings.

Working with Virtual Machines

Many of the most useful scripts you’ll write will for performing tasks with Virtual Server VMs. You do this by creating a virtual machine object and then setting it equal to a particular VM. If you know the name of a particular VM, you can use the following syntax:

Set objVirtualServer = CreateObject(“VirtualServer.Application”)

objVirtualMachine = objVirtualServer.FindVirtualMachine(“Windows XP SP2”)

First, you create a Virtual Server object (as we did in the previous section), and then you use its find method to create a virtual machine object. What if you don’t know the name of the VM you want to access? You can easily loop through the VMs in the Virtual Server object’s “VirtualMachines” collection. Here’s a sample that outputs the name of each VM on the server:

Set objVirtualServer = CreateObject(“VirtualServer.Application”)

For Each objVirtualMachine in objVirtualServer.VirtualMachines

WScript.Echo(“Name of VM: “) & objVirtualMachine.Name

Next

Once you have a “handle” to a virtual machine, you can access and modify its properties. One commonly-used function is to determine the amount of memory attached to a VM:

WScript.Echo “VM Memory: “ & objVirtualMachine.Memory

Of course, you can perform useful tasks like starting or stopping the VM and managing the virtual hardware configuration (those are topics that we’ll cover in later articles in this series).

The End of the Beginning

In this article, I provided a few simple lines of VBScript code that allowed you to connect to Virtual Server and to work with VMs. Admittedly, we haven’t done anything that’s useful in a practical sense just yet. The goal of this article was to lay the foundation for getting started with automation. Next in the series: Automating Virtual Server using .NET.