Archive for category Automating / Scripting

Windows Server Software-Defined Storage and Networking Presentation: Dec. 5th, 2014

image

I’m excited to have been given the opportunity to present at the December meeting of the Central Texas Systems Management User Group (CTSMUG)!  I’ll try to post some more details on the topic here within the next week. 

The meeting starts at 10:00am and includes lunch.  For details (including directions and a full agenda) and to register to attend, see the Event Details.  There are lots of other interesting topics on the agenda, so do try to attend if you’re in the Austin area!

Austin Code Camp 2013 SQL Server Presentations

If you’re in the Austin area and are interested in a free all-day conference on .NET development-focused topics, be sure to check out the Austin .NET User Group’s annual Austin Code Camp.  It takes place on Saturday, August 17th at the St. Edward’s Professional Education Center in Austin.  See the site for details on the speakers, sessions, and location. 

This year, I’ll be presenting on a couple of topics that were well-receiving in past years.  The sessions will be demo-focused, though you can also find the slides from my past presentations on this site.  Feel free to let me know if you have any questions or comments!

SQL Server Performance Monitoring & Optimization

Developers can dramatically improve performance by understanding how their queries are executing in the "real world". This session will cover ways in which attendees can monitor performance at various levels, and how they can use this information to optimize queries and improve overall application performance. We’ll start with a discussion of developing a performance optimization strategy and how various tools can help. Then, we’ll walk through the process of using SQL Profiler to collect data in a real-world case: Generating a "hit list" of worst-performing queries based on execution times and frequency.

Next we’ll look at using the Database Tuning Advisor to make suggestions related to physical database structures such as indexes and partitions. Also included will be ways in which built-in reports and dynamic management views can be used to monitor performance of production systems. Time permitting, the presentation will include methods for reducing deadlocks and managing long-running transactions. Finally, the presentation will include an overview of analyzing query plans. The overall emphasis will be on solving practical, real-world database performance problems.

SQL Server Reporting Services: Creating and Managing Reports

Using SQL Server Reporting Services, developers can author and distribute complex reports that come from a variety of data sources. The session will begin with an architectural overview of Reporting Services and how developers can install and configure the required services. Then, we will walk through the process of creating new reports by building connections and data sets using Report Builder 3.0 and Visual Studio 2012. Reporting design features including dynamic drill-downs, tablix reports, charts, and sorting will be demonstrated. Also included will be methods by which multiple levels of report parameters can be used to filter data and increase performance.

Next, we’ll look at deployment details, including scheduling report execution, report subscriptions, configuring caching, creating snapshots, and managing security. Time permitting, the presentation will include a demonstration of using Report Viewer controls within ASP.NET and Windows Forms applications.

Virtual Platform Management – Data Center Automation

A new chapter from my eBook titled The Definitive Guide to Virtual Platform Management is now available for free download (registration is required).  Chapter #9, "Data Center Automation", focuses on ways in which enterprise management tools can help make the seemingly insurmountable task of managing server sprawl and VM sprawl much easier.  Here’s a brief excerpt from the introduction:

A constant challenge in most IT environments is that of finding enough time and resources to finish all the tasks that need to be completed. IT departments find themselves constantly fighting fires and responding to a seemingly never-ending stream of change requests. Although virtualization technology can provide numerous advantages, there are also associated management-related challenges that must be addressed. When these tasks are performed manually, the added overhead can reduce cost savings and can result in negative effects on performance, availability, reliability, and security.

In previous chapters, I have covered a broad array of best practices related to virtualization management. Organizations have the ability to choose from a range of implementation methods, including physical servers, virtual machines, and clustered systems. The tasks have ranged from deployment and provisioning to monitoring virtual systems once they are in production. All of this raises questions related to the best method of actually implementing these best practices.

The focus of this chapter is on data center automation. Organizations that have deployed virtual machines throughout their environment can benefit from using enterprise software that has been designed to provide automated control. The goal is to implement technology that can provide for a seamless, self-managing, and adaptive infrastructure while minimizing manual effort. It’s a tall order, but certainly one that is achievable by using a combination of best practices and the right tools.

Stay tuned for the next and final chapter of the Guide!

Automating Virtual Server, Part 4: Creating and Managing VMs

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!

Automating Virtual Server, Part 3: Configuring the Virtual Server Service

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.

image

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.

image

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!

Automating Virtual Server, Part 2: Automating Virtual Server Using .NET

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.

image

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).

image

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!

Automating Virtual Server, Part 1: Automating Virtual Server Using VBScript

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

Series Introduction

Among the many benefits of deploying virtualization technology is the ability to better manage complex environments. When implemented correctly, it’s often easier to manage virtual machines than it is to keep track of physical ones. But what happens when you end up with a large number of VMs. Organizations that start by dabbling in virtualization often find that the number of virtualization hosts and guest OS’s can quickly get out-of-hand. Add to that the fact that virtualization management tools are still evolving, and you can end up with quite a challenge for systems administrators.

When dealing with physical servers, many IT pros and end-users have found ways to use automated methods of handling otherwise tedious tasks. For example, performing backups and deploying operating system updates are generally done manually in only the smallest of environments.

When it comes managing Microsoft Virtual Server VMs, there’s a readily available method for automation. In this series of articles, Automating Virtual Server, I’ll present details and examples of how you can programmatically manage your VMs. Code samples will be provided in VBScript, Visual Basic .NET, and C# syntax. Some experience with scripting and application development is assumed, but I’ll provide links to relevant information if you need to get up to speed. Overall, I think you’ll find that the process is fairly easy and straight-forward.

Automating Virtualization

If you’re a systems administrator or developer that’s faced with managing a large number of virtual machines, there’s a good chance that you find yourself wishing for a robot that could handle some of the more mundane tasks. While building such a device is more than could be covered in a single article, the focus here is to demonstrate how you can create scripts for performing routine tasks in Virtual Server. We’ll begin by using the VBScript language in this article, since it’s the easiest way to get up and running. In later articles, I’ll provide code samples in .NET languages (C# and Visual Basic.NET).

Virtual Server’s COM API

One of the built-in features of Microsoft Virtual Server is a fully-documented Component Object Model (COM) API. By connecting to this API, you can perform pretty much any task that can be carried out manually using Virtual Server’s built-in tools. In fact, the Virtual Server Administration Web Site itself is simply a web-based user interface that connects to the COM API behind the scenes.

The basic structure of the COM API is based on collections. Figure 1 provides an overview of the some of the most commonly-used ones. The figure is not a complete object model, and the full details can be found in the Virtual Server Programmer’s Guide (a help file that’s automatically installed with the product). The Programmer’s Guide includes details about all of the objects that are available, along with a list of properties, methods and enumeration constants. If you’re doing more than basic scripting, you’ll probably find yourself referring to that file often.

image

Figure 1: An overview of the Virtual Server COM object model

Getting Started with VBScript

It’s beyond the scope of this article to cover the details of the VBScript language. If you’re not familiar with it, some good starting points for more information are the Microsoft Developer Network (MSDN) Scripting site (http://msdn.microsoft.com/scripting) and the Microsoft TechNet Script Center (http://www.microsoft.com/technet/scriptcenter).

VBScript is based on the Visual Basic language syntax, and it’s a very simple language for getting started with automation. The language is not case-sensitive and it provides for automatic data type conversions. These are two “features” that can help get you up and running quickly (though they’re not good practices when writing production applications).

Another advantage of writing VBScript code is that there’s no requirement for a development environment: Just use your favorite text editor. In most cases, you can copy and paste the code in this article and use it directly on a Windows computer that’s running Virtual Server. OK, let’s get started.

Connecting to Virtual Server

Generally, the first programmatic step you’ll take when creating a new VBScript file for automating Virtual Server is to create a new object that attaches to the COM API. The following statement takes care of this:

Set objVirtualServer = CreateObject(“VirtualServer.Application”)

As long as the Virtual Server product is installed on the local computer, this script should run as-is. By default, this will connect to the instance of Virtual Server that is running on the local machine. If you want to connect to an instance of Virtual Server that’s running on a remote computer, you can use a link such as the following:

Set objVirtualServer = CreateObject(“VirtualServer.Application”,”RemoteServerName”)

Note that you’ll need to have the appropriate security settings and configuration to be able to remotely connect to a COM object (you can get details on these topics using the links provided earlier).

Let’s prove that everything’s working by adding the following two lines to get some information about the local Virtual Server instance:

WScript.Echo “Server Name: “ & objVirtualServer.Name

WScript.Echo “Server Uptime (sec): “ & objVirtualServer.Uptime

Now, the script will output the name of the instance (which should be the same as the machine name), along with the number of seconds that the Virtual Server service has been running. We did this by accessing properties of our “objVirtualServer” object. You can use other methods and properties of the Virtual Server object to perform tasks such as programmatically creating virtual hard disks and changing server settings.

Working with Virtual Machines

Many of the most useful scripts you’ll write will for performing tasks with Virtual Server VMs. You do this by creating a virtual machine object and then setting it equal to a particular VM. If you know the name of a particular VM, you can use the following syntax:

Set objVirtualServer = CreateObject(“VirtualServer.Application”)

objVirtualMachine = objVirtualServer.FindVirtualMachine(“Windows XP SP2”)

First, you create a Virtual Server object (as we did in the previous section), and then you use its find method to create a virtual machine object. What if you don’t know the name of the VM you want to access? You can easily loop through the VMs in the Virtual Server object’s “VirtualMachines” collection. Here’s a sample that outputs the name of each VM on the server:

Set objVirtualServer = CreateObject(“VirtualServer.Application”)

For Each objVirtualMachine in objVirtualServer.VirtualMachines

WScript.Echo(“Name of VM: “) & objVirtualMachine.Name

Next

Once you have a “handle” to a virtual machine, you can access and modify its properties. One commonly-used function is to determine the amount of memory attached to a VM:

WScript.Echo “VM Memory: “ & objVirtualMachine.Memory

Of course, you can perform useful tasks like starting or stopping the VM and managing the virtual hardware configuration (those are topics that we’ll cover in later articles in this series).

The End of the Beginning

In this article, I provided a few simple lines of VBScript code that allowed you to connect to Virtual Server and to work with VMs. Admittedly, we haven’t done anything that’s useful in a practical sense just yet. The goal of this article was to lay the foundation for getting started with automation. Next in the series: Automating Virtual Server using .NET.