Managing virtual machines - Automating Microsoft Virtual Server

Chapter 4: Creating and managing virtual machines

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

If you've been following along with my series on Automating Microsoft Virtual Server

    Requires Free Membership to View

so far, you're familiar with the process of connecting to an instance of Virtual Server and accessing a virtual machine object (either by name or by looping through a collection of all VMs on the server). I provided examples of code in VBScript, Visual Basic .NET, and C#. In this article, I'll walk through the process of creating new VMs, and managing some basic VM (virtual machine) properties.

Creating a new virtual machine
Since the point of virtualization is to run VMs, it should come as no surprise that the Virtual Server object contains a collection of virtual machines. Let's walk through the most common operations related to working with VMs. When writing code, you'll generally first create an instance of a Virtual Server host object by connecting to the host. To create a new VM, you can use the following method:

.CreateVirtualMachine(configurationName, configurationPath)

You need only provide two arguments to this method:

  • configurationName: A string value that specifies the logical name of the new virtual machine. The name must be unique on each instance of Virtual Server, so it's a good idea to check the .VirtualMachines collection for potential conflicts. Note that this name will also be used to create the default filenames of the VM.

  • configurationPath: A string that specifies the full path to the host folder into which the new VM files will be placed. It's a good idea to test that the Virtual Server service can access this path. If the specific folder does not exist, the Virtual Server COM API will create it automatically.

It's important to keep in mind that different instances of Virtual Server might have different configuration settings. Remember that you can get information about default paths and search paths by using properties of the Virtual Server object.

Deleting a VM
From time to time, it will be necessary to delete a virtual machine and its associated files. The code requires a call to a method of the Virtual Server object:


The argument here is actually a virtual machine object. So, your code will need to get an instance of a VM object by looping through the .VirtualMachines collection or by finding it by name. There's one important warning: Calling this method will actually result in the deletion of the relevant virtual machine configuration (.vmc) file, along with any undo disks or saved state files. Use with caution!

Registering VMs
Adding and removing virtual machines from the Virtual Server instance is also a simple process. Registration involves pointing Virtual Server to an existing virtual machine configuration (.vmc) file. Virtual Server will try to enumerate the virtual hard disks and other properties stored in that file and add the VM with a given name. The method call is:

.RegisterVirtualMachine(configurationName, configurationPath)

Note that the syntax is similar to that of creating a new VM – you need to provide the logical name that will be used for the VM, along with the full path and filename of the .vmc file.

Unregistering VMs
If you want to remove a virtual machine from Virtual Server without deleting the associated files, you can use this method:


As with deleting virtual machines, you need to provide an instance of a virtual machine object. This method is handy when you want to move or copy a virtual machine to another server.

Code examples: Managing virtual machines
So far, we've looked at some pseudo-code that shows you how to perform common operations related to adding and removing VMs. Now, let's look at specific working examples. The following code will perform these steps:

  1. Create a new VM called "VSAutomation". The files will be stored in "C:\VM\VSAutomation" (note that you'll most likely want to change this path if you plan to run the code yourself).
  2. Unregister this VM from the Virtual Server installation.
  3. Copy all VM-related files to a new folder: VSAutomation2.
  4. Register the new VM in Virtual Server under the logical name "VSAutomation2".

Here are the code samples in VBScript, Visual Basic .NET and C# formats, respectively:

Set objVirtualServer = 

'Create a new VM named "VSAutomation "
    "VSAutomation", "C:\VM\VSAutomation"

'Get a Virtual Machine object
Set objVirtualMachine =

'Unregister the new VM

'Copy the Virtual Machine files to 
    the folder "VSAutomation2"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFolder "C:\VM\VSAutomation",
    "C:\VM\VSAutomation2", OverWriteFiles

'Register the new copy of the virtual machine
objVirtualServer.RegisterVirtualMachine "VSAutomation2", 

WScript.Echo "Operation complete."

Listing 1: Managing virtual machines using VBScript

Imports Microsoft.VirtualServer.Interop

Namespace SearchServerVirtualization_VB
    Public Class Listing_2
        Public Shared Function
        ManageVirtualMachines() As String
            Dim objVirtualServer As New VMVirtualServer

            'Create a new VM named "VSAutomation"
                " C:\VM\VSAutomation")

            'Get a Virtual Machine object
            Dim objVirtualMachine As VMVirtualMachine
            objVirtualMachine =

            'Unregister the new VM

            'Copy the Virtual Machine files
                 to the folder "Test-Copy"

            'Register the new copy of the virtual machine

            Return "Operation complete."
        End Function
    End Class
End Namespace

Listing 2: Managing virtual machines using VB.NET

using Microsoft.VirtualServer.Interop;

namespace SearchServerVirtualization_CSharp
   namespace ScriptingVirtualServer
      public class Listing_3
         public static string ManageVirtualMachines()
            VMVirtualServer objVirtualServer
               = new VMVirtualServer();
            //Create a new VM named "VSAutomation"
                ("VSAutomation", @"C:\VM\VSAutomation");
            //Get a Virtual Machine object
            VMVirtualMachine objVirtualMachine;
            objVirtualMachine =
            //Unregister the new VM
            //Create a new folder called "Test-Copy"
                 //and move the .vmc file
                    @" C:\VM\VSAutomation2\VSAutomation2.vmc ");

            //Register the new copy of the virtual machine
                   @"C:\VM\VSAutomation2\VSAutomation2.vmc ");
            return "Operation complete.";

Listing 3: Managing Virtual Machines using C#

In this article, I walked through details related to creating new VMs and managing VM registration in Virtual Server. It only takes a little imagination to see how the code samples could be used to programmatically move and copy VMs between installations of Virtual Server. While I left out important aspects such as error-handling, logging, security-checking, and other details, this should serve as a great foundation for performing useful automation tasks.

In the next article in this series, I'll provide details about how you can work with VMs, including diving down into their virtual hardware configurations. 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).

This was first published in March 2007

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.