This article was first published on SearchServerVirtualization.TechTarget.com.

If you’ve been following along with my series on “Automating Virtual Server” 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 properties.

Creating a new VM

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 Virtual Machine

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:

.DeleteVirtualMachine(VirtualMachineObject)

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 the 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:

objVirtualServer.UnregisterVirtualMachine(VirtualMachineObject)

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 = CreateObject(“VirtualServer.Application”)

‘Create a new VM named “VSAutomation ”

objVirtualServer.CreateVirtualMachine “VSAutomation”, “C:\VM\VSAutomation”

‘Get a Virtual Machine object

Set objVirtualMachine = objVirtualServer.FindVirtualMachine(“VSAutomation”)

‘Unregister the new VM

objVirtualServer.UnregisterVirtualMachine(objVirtualMachine)

‘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”, “C:\VM\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”

objVirtualServer.CreateVirtualMachine(“VSAutomation”, ” C:\VM\VSAutomation”)

‘Get a Virtual Machine object

Dim objVirtualMachine As VMVirtualMachine

objVirtualMachine = objVirtualServer.FindVirtualMachine(“VSAutomation”)

‘Unregister the new VM

objVirtualServer.UnregisterVirtualMachine(objVirtualMachine)

‘Copy the Virtual Machine files to the folder “Test-Copy”

My.Computer.FileSystem.CopyDirectory(“C:\VM\VSAutomation”, “C:\VM\VSAutomation2”)

‘Register the new copy of the virtual machine

objVirtualServer.RegisterVirtualMachine(“VSAutomation2”, “C:\VM\VSAutomation2”)

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”

objVirtualServer.CreateVirtualMachine(“VSAutomation”, @”C:\VM\VSAutomation”);

//Get a Virtual Machine object

VMVirtualMachine objVirtualMachine;

objVirtualMachine = objVirtualServer.FindVirtualMachine(“VSAutomation”);

//Unregister the new VM

objVirtualServer.UnregisterVirtualMachine(objVirtualMachine);

//Create a new folder called “Test-Copy” and move the .vmc file

System.IO.Directory.CreateDirectory(@”C:\VM\VSAutomation2″);

System.IO.File.Move(@”C:\VM\VSAutomation\VSAutomation.vmc”, @” C:\VM\VSAutomation2\VSAutomation2.vmc “);

//Register the new copy of the virtual machine

objVirtualServer.RegisterVirtualMachine(“VSAutomation”, @”C:\VM\VSAutomation2\VSAutomation2.vmc “);

return “Operation complete.”;

}

}

}

}

Listing 3: Managing Virtual Machines using C#

Summary

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!