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

How to configure the Microsoft Virtual Server service

In this article, Anil Desai presents ways you can programmatically manage properties and details related to the Virtual Server service itself.

Chapter 3: Configuring the Virtual Server service

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

In the first two articles in this series on "Automating Virtual Server," I presented details on connecting to an instance of Virtual Server using its COM API. The first focused on accomplishing this using VBScript, and the second focused on using .NET (Visual Basic .NET and C#). The articles stopped a little short of doing anything useful once you've connected to a Virtual Server instance. So, that's where I'll resume. In this article, I'll present ways in which you can programmatically manage properties and details related to the Virtual Server service itself.

The Virtual Server object
Understanding the relationships between the major objects and collections is an important first step when working with the Virtual Server COM API. The Virtual Server object represents the host computer's virtualization service. It contains properties and methods related to the configuration of the virtualization layer. Table 1 provides some examples of commonly-used properties.

If you're writing script or application code, these properties can be very helpful. For example, you might want to determine the default path into which a new VM would be placed (which returned by the .DefaultVMConfigurationPath property). Or, you might want to reference the .Version property so you can execute different sections of code based on the specific version of the Virtual Server platform that's running on the system. Finally, the .GetVirtualMachineFiles method allows you to take a quick inventory of all of the VM-related files that are available within the default search paths.

Getting host details
When programmatically working with Virtual Server, it's important to keep track of the details of the platform you're managing. One particularly useful collection for returning these details is the .HostInfo property of a Virtual Server object. Table 2 provides a listing of the information you can obtain.

Code examples: Getting host information
Now that we have an understanding of what information is available, let's write some simple code that will return the following:

  1. Total Uptime for the Virtual Server instance (returned in seconds)
  2. The Operating System platform
  3. The number of CPUs
  4. The CPU Speed

The following Listings provide code samples in VBScript, VisualBasic.NET and C# formats, respectively.

Set objVirtualServer = 
Set objHostInfo = objVirtualServer.HostInfo

'Generate the output string
Output = "Uptime (sec): " & objVirtualServer.Uptime
Output = Output & vbCrLf & 
       "Host OS: " & objHostInfo.OperatingSystem
Output = Output & vbCrLf & "# of CPUs: 
       " & objHostInfo.PhysicalProcessorCount
Output = Output & vbCrLf & "CPU Speed: 
       " & objHostInfo.ProcessorSpeedString

WScript.Echo Output

Listing 1: Getting Virtual Server Host info using VBScript

Imports Microsoft.VirtualServer.Interop

Namespace SearchServerVirtualization_VB
    Public Class Listing_2
        Public Shared Function GetHostInfo() As String
            Dim objVirtualServer As New VMVirtualServer
            Dim Output As String = ""
            Output = "Uptime (sec): " & 
            Output &= vbCrLf & "Host OS: " & 
            Output &= vbCrLf & "# of CPUs: " & 
            Output &= vbCrLf & "CPU Speed: " 
                 & objVirtualServer.HostInfo.ProcessorSpeedString
            Return Output
        End Function
    End Class
End Namespace

Listing 2: Getting Virtual Server Host info using VB.NET

using Microsoft.VirtualServer.Interop;

namespace SearchServerVirtualization_CSharp
   namespace ScriptingVirtualServer
      public class Listing_3
         public static string GetHostInfo()
            VMVirtualServer objVirtualServer 
                 = new VMVirtualServer();
            string Output = "";
            Output = "Uptime (sec): " 
                 + objVirtualServer.UpTime.ToString();
            Output += "\r\n" + "Host OS: " + 
            Output += "\r\n" + "# of CPUs: " + 
            Output += "\r\n" + "CPU Speed: " 
                 + objVirtualServer.HostInfo.ProcessorSpeedString;
            return Output;

Listing 3: Getting Virtual Server Host info using C#

Coming up next…
Covering details of the Virtual Server object paves the way for the focus of my next article: creating and managing virtual machines. 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.