Problem solve Get help with specific problems with your technologies, process and projects.

Using VBScript to automate Microsoft Virtual Server

Learn how to create scripts for performing routine tasks in Microsoft Virtual Server with this new series by Anil Desai. In part one, he tells you how with VBScript.

Chapter 1: Introduction to automating virtualization

In this series, "Automating Microsoft Virtual Server," Anil Desai describes ways to programmatically manage virtual machines.

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 (OS) updates are generally done manually in only the smallest of environments. Fortunately, there are ways to automate the management of virtual machines (VMs), too. This series will provide a guide to automating Microsoft Virtual Server.

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 get started in virtualization often find that the number of virtualization hosts and guest OSes 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.

Throughout this series, 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 who is faced with managing a large number of VMs, there's a good chance that you'll find yourself wishing for a robot that could handle some of the more mundane tasks. While I can't tell you how to build such a device 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.


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 and the Microsoft TechNet Script Center.

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: Using Visual Studio .NET to automate Microsoft Virtual Server.

About the author: Anil Desai is an independent consultant based in Austin, Tex. He specializes in evaluating, implementing and managing solutions based on Microsoft technologies. He has worked extensively with Microsoft's Server products and the .NET development platform and has managed datacenter environments that support thousands of virtual machines. Anil is an MCSE, MCSD, MCDBA and a Microsoft MVP (Windows Server -- Management Infrastructure).

This was last published in January 2007

Dig Deeper on Virtual machine performance management

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close