This article was first published on

The .NET Advantage

In the first article in this series, Automating Virtual Server Using VBScript, I provided an overview of Virtual Server’s COM API. If you’re unfamiliar with the object model, I recommend you read that article before continuing with this one. To get started quickly, I presented examples in VBScript format. While this was a simple way to get started, many developers and systems administrators will want to use the features of a type-safe, compiled language for their production automation needs. In this article, I’ll walk through the process of getting started with Virtual Server’s COM API using Visual Studio .NET. And, I’ll provide code samples in both C# and Visual Basic.NET.

Creating a .NET Application

In order to make the most of the topics in this article, you’ll need to be familiar with developing simple .NET applications using Visual Studio. If you don’t consider yourself at least a closet developer, I recommend you stick with using VBScript (at least to being with). If you’d like more information about Microsoft’s .NET development technologies, a great starting place is the Microsoft Developer Network (MSDN) .NET site at

There’s one important requirement that you’ll need to keep in mind when automating Virtual Server using .NET: You must have your COM security settings configured so that your applications will be able to access either the local instance of Virtual Server, or an instance running on another computer. Details related to doing this programmatically are covered in the Virtual Server Programmer’s Guide (a help file that’s automatically installed with Virtual Server). While you’re there, be sure to read details related to threading recommendations.

OK, now that we have the preliminary information out of the way, let’s get started by looking at code that can be using in a C# or VB.NET application. These examples can be used in any type of .NET application, including Windows Forms applications, ASP.NET web applications, command-line applications, and Windows Services.

Checking Your References

Once you’ve created a new .NET project, you’ll need to add a reference to the Virtual Server 2005 R2 Type Library. If you’re using Visual Studio, you can easily to this by clicking on Project à Add Reference, and then selecting the COM tab (see Figure 1). As long as Virtual Server is installed and registered on the local machine, you should see the type library listed.


Figure 1: Adding a reference to the “Virtual Server 2005 R2 Type Library” COM object.

Since the type library is a COM object (as opposed to a .NET-based “managed code” object), Visual Studio will automatically create a COM interoperability layer. The bottom line is that you’ll now be able to access the API by adding one of the following lines to your source code (I’ll use the convention of presenting Visual Basic.NET code samples first, followed by C#):

Imports Microsoft.VirtualServer.Interop ‘Visual Basic Example

using Microsoft.VirtualServer.Interop; ‘ C# Example

Connecting to Virtual Server

Most commonly, you’ll start writing your code by creating a Virtual Server object. Here are some code samples for connecting to the local instance of the Virtual Server service:

‘ Visual Basic Example

Dim objVirtualServer As VMVirtualServer

objVirtualServer = New VMVirtualServer

‘C# Example

VMVirtualServer objVirtualServer;

objVirtualServer = new VMVirtualServer();

You’re now free to start working with the properties and methods of the Virtual Server object. To get more details (as well as details about connecting to remote instances of Virtual Server), see the Virtual Server Programmer’s Guide.

Working with Virtual Machines

The most common next step in typical Virtual Server automation code will be to create an object that references a specific virtual machine. This process is easy enough and can be done in a couple of different ways. First, if you know the name of the VM for which you want to create an object, you can simply use the .FindVirtualMachine() method of the Virtual Server object that you’ve created (just provide the name of the VM as a string argument).

If you want to loop through all of the VMs on a particular instance of Virtual Server, you can simply iterate through the .VirtualMachines collection (a property of the Virtual Server object itself). One item will be returned for each VM that is attached to the host. Once you have a reference to a particular VM, you can access properties of the VM object to get and set properties such as the amount of physical memory that’s allocated. And, you can use methods to perform operations such as starting and stopping VMs, and adding and removing virtual hardware. We’ll focus on those topics in later articles in this series.

Looking to the Future

So far, we’ve focused on the first steps that are required to get started with building a basic Virtual Server automation application using .NET. Just as a quick example, the process for building a Windows forms application that performs some of the same operations as Virtual Server’s built-in admin tools can be quick and easy. While it’s not exactly a state-of-the-art example of coding finesse, I created a simple but functional demonstration application (shown in Figure 2) in under an hour. And, I chose not to use my favorite .NET language (I’ll let you guess which one that is).


Figure 2: A simple Virtual Server automation application.

The total functional portion of the source code is somewhere around 200 lines (you can download the Visual Studio project from my web site at Note that the source code is provided as-is, and it’s only provided as an example – it most certainly isn’t production-quality code.

Update: You can now download the source code directly using the following link: VirtualServerTestApp (Anil Desai).  The file includes a zip file with a Visual Studio 2008 project.  Be sure to see the earlier posts in this series to determine the pre-requisites for running the application.

Building on the Foundation

In this and the previous article, I’ve set the stage for performing more useful tasks. You should now have the ability to connect to an instance of Virtual Server and to create an object for a virtual machine. In the next several articles, I’ll provide details for managing virtual machines, working with virtual hard disks, and configuring virtual networks. We’re just getting warmed up, so stay tuned!