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

Using Visual Studio .NET to automate Microsoft Virtual Server

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

Chapter 2: Getting started with COM API

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

Managing a few virtual machines (VMs) manually is no big deal. Once you've got dozens or hundreds of VMs, you'll gain greater control by automating as many management tasks as possible. When it's time to automate Microsoft Virtual Server, you'll find a friend in Visual Studio .NET.

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.

In the first article in this series, Using VBScript to automate Microsoft Virtual Server , 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.

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 Website.

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", then selecting "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, AnilDesai.net). 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.

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!

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).

Dig Deeper on Microsoft Hyper-V and Virtual Server

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