This article was first published on SearchServerVirtualization.TechTarget.com.
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 http://msdn.microsoft.com/net.
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 http://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.
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!
#1 by Maddy on February 27, 2008 - 5:13 am
Quote
Hi,
Even i am doing a same project in C# for automating VMWARE ESX Server. In this project i am supposed to transfer an VMX image to a remote system and then start the image and test various operations like start, suspend, hibernate, restart.
Also i need to install new apps and try it. I have done most of the code and was wondering if you have any PPT or PDF which can help me as a report for the same.
#2 by Anil Desai on February 27, 2008 - 8:42 am
Quote
Maddy,
While I don’t have any specific experience with automating VMware ESX, you should be able to find several articles that cover the topic at http://searchvmware.techtarget.com/. Andrew Kutz and other writers are familiar with the process for accessing VMware’s APIs. You can also find more information on Andrew’s web site at http://www.lostcreations.com/code/wiki/vmware. I hope this is helpful. Good luck!
– Anil
#3 by Sumit on November 12, 2008 - 8:08 am
Quote
Hi Anil,
I have to automate the process of VM creation and maintenance through VB.NET. Your articles and web casts have been very helpful. An example would be very helpful to get me started on this. But I am unable to download the VS application mentioned above.
Can you provide the url to download page.
Thanks.
#4 by Anil Desai on November 12, 2008 - 8:59 am
Quote
Sumit,
It’s somewhat cleverly hidden on my web site, but you can download presentations, source code, and a sample application from this page:
https://anildesai.net/Main/Technical/Virtualization/VirtualServerPresentations/tabid/73/Default.aspx
I tend to code primarily in VB.NET, but I wrote this sample code in C# because I was presenting to a local users’ group. I hope this is helpful!
#5 by Alex on September 21, 2009 - 4:46 pm
Quote
developing with VS2008 on my Vista machine, I am finding this info to be outdated. For example, there’s no “Virtual Server” COM object in my list of references.
#6 by Anil Desai on September 21, 2009 - 5:02 pm
Quote
Alex – you’re right about some of this being outdated. Microsoft Virtual Server is no longer a current product, and it has been replaced largely with Hyper-V and Windows Virtual PC (for Vista and Windows 7). Still, if you install and configure MSVS on a computer, you should be able to reference the COM object. You can also manually browse to it if that’s what you’re trying to do. My recommendation would be to move to Hyper-V if that’s at all possible. I hope this helps!
#7 by Deepak on February 17, 2010 - 4:44 am
Quote
I clicked the following link provided by you
https://anildesai.net/Main/Technical/Virtualization/VirtualServerPresentations/tabid/73/Default.asp
but the link is not working
plz give an alternate download link
#8 by Anil Desai on February 17, 2010 - 8:55 am
Quote
Deepak: I currently don’t have direct links to the presentation and source code listed here. However, if you send me an e-mail (Anil@AnilDesai.net), I’d be happy to send that content to you directly.
#9 by Rassurbalalse on March 6, 2010 - 9:37 pm
Quote
Thanks for writing, I very much liked your newest post. I think you should post more frequently, you evidently have natural ability for blogging!
#10 by Samselvaprabu on March 11, 2010 - 1:42 am
Quote
Can you please send the source code and presentation to my email id. as you have mailed to Deepak . thanks in advance
#11 by Anil Desai on March 11, 2010 - 7:21 am
Quote
#10: Sure. I have just sent a zip file with source code via e-mail.
#12 by Anil Desai on March 11, 2010 - 7:22 am
Quote
#10: Actually, I received an immediate bounce when sending to your e-mail address (apparently the attachment type is not allowed). I have updated this post to include a link to the source code (see the section titled “Update”). I hope this is helpful! – Anil
#13 by Samselvaprabu on March 12, 2010 - 2:10 am
Quote
Thanks Dude for your quick response.