This article was first published on SearchServerVirtualization.TechTarget.com.
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.
Table 1: Commonly-used properties of the Virtual Server object
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.
Table 2: Properties of the .HostInfo collection
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 = CreateObject(“VirtualServer.Application”)
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): ” & objVirtualServer.UpTime.ToString
Output &= vbCrLf & “Host OS: ” & objVirtualServer.HostInfo.OperatingSystem.ToString
Output &= vbCrLf & “# of CPUs: ” & objVirtualServer.HostInfo.PhysicalProcessorCount.ToString
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: ” + objVirtualServer.HostInfo.OperatingSystem.ToString();
Output += “\r\n” + “# of CPUs: ” + objVirtualServer.HostInfo.PhysicalProcessorCount.ToString();
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!
#1 by Samselvaprabu on March 11, 2010 - 5:43 am
Quote
when executing the code given, it is throwing following error.
Unhandled Exception: System.Runtime.InteropServices.COMException (0x80070542): E
ither a required impersonation level was not provided, or the provided impersona
tion level is invalid. (Exception from HRESULT: 0x80070542).
How to avoid this?
#2 by Anil Desai on March 11, 2010 - 7:24 am
Quote
#1: It sounds like the problem is related to the COM configuration settings on your computer. The first article in this series provides in-depth details on how to configure impersonation levels so your application can access the necessary APIs. – Anil