In the past couple of posts on this I took you through creating virtual machines and backing them up. In this section I am going to concentrate of controlling the virtual machines. In Part 4 I will look at some of the extended features available and beyond.. well I will let you wait and see.

So, here goes...

To start... we need to know how to retrieve a list of virtual machines that are on the system. There are a couple ways of doing this, but I like to retrieve basic information about the state of each Virtual Machine as well as just it's name.

There main command we are going to use here is:

objVM.State - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msvs/msvs/ivmvirtualmachine_state.asp

 

-------------------------------------------------------------------------

On Error Resume Next

' A script for enumerating virtual machines

' Get / Set arguments
vsHost = "localhost"
'vsHost = WScript.Arguments(0)

Set objVS = CreateObject("VirtualServer.Application",vshost)
set colVMs = objVS.VirtualMachines
set outputtext = "VM Name : State" & chr(13)

For Each objVM in colVMS
 cstate = objVM.State

 If cstate = 1 then
  tstate = "off"

 elseif cstate = 2 then
  tstate = "saved" 

 elseif cstate = 3 then
  tstate = "starting" 

 elseif cstate = 4 then
  tstate = "restoring" 

 elseif cstate = 5 then
  tstate = "running" 

 elseif cstate = 6 then
  tstate = "paused" 

 elseif cstate = 7 then
  tstate = "saving" 

 elseif cstate = 8 then
  tstate = "turning off" 

 else tstate = "unknown"

 end if
 outputtext = outputtext & objVM & ": " & tstate & chr(13)

Next

Wscript.Echo outputtext

-------------------------------------------------------------------------

Next we want to be able to change the state, so here are a couple of useful scripts:

Turn the VM on:

' A script for starting a virtual machine

' Get / Set arguments
vmName=WScript.Arguments(0)
vsHost = "localhost"
'vsHost = WScript.Arguments(1)

'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application", vshost)

'Set virtual machine from command-line parameter
set vm = virtualServer.FindVirtualMachine(vmName)
Wscript.Echo "Starting Virtual Machine: " & vmName

'Start Virtual Machine
vm.Startup

-------------------------------------------------------------------------

Turn the VM Off

' A script for stopping a virtual machine

' Get / Set arguments
vmName=WScript.Arguments(0)
vsHost = "localhost"
'vsHost = WScript.Arguments(1)

'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application", vsHost)

'Set virtual machine from command-line parameter
set vm = virtualServer.FindVirtualMachine(vmName)
Wscript.Echo "Stopping Virtual Machine: " & vmName

'Turn off virtual machine and wait for it to finish turning off
vm.TurnOff

-------------------------------------------------------------------------

Save the state of the VM

' A script for saving a virtual Machine State

' Get / Set arguments
vmName=WScript.Arguments(0)
vsHost = "localhost"
'vsHost = WScript.Arguments(1)

'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application", vsHost)

'Set virtual machine from command-line parameter
set vm = virtualServer.FindVirtualMachine(vmName)
Wscript.Echo "Saving State of Virtual Machine: " & vmName

'Save state the virtual machine
set saveTask = vm.Save

Watch this space for the next installment.

Enjoy!