Starting Virtual Machines via Scripting with Windows Virtual PC

Here are some simple scripts that allow you to start a virtual machine with Windows Virtual PC.  Now, you may be wondering why you would want to do this.  Well there are a couple of reasons I can think of:



  • Automated testing.  If you have a virtual machine that runs an automated tests, or a daily batch job: use a script to run the virtual machine in the background.

  • Patch deployment.  If you have an environment where patches are deployed after-hours – these scripts can ensure that virtual machines are running at the right time to receive the updates.

  • Faster launch. A virtual machine that is running in the background can still be used for seamless applications or for full-desktop virtual machine usage.  If you have a virtual machine that you regularly use, starting it with a script when you login means that it will be ready to go as soon as you need it.

PowerShell:



param([string]$vmName)
 
# Check for correct command-line arguments
If ($vmName -eq “”)
 {
 write-host “Missing command-line argument.”
 write-host “USage: StartVM.ps1 -vmName `”Name of virtual machine`“”
 exit
 }
 
# Connect to Virtual PC
$vpc=new-object –com VirtualPC.Application –Strict
 
# Get virtual machine object
$vm = $vpc.FindVirtualMachine($vmName)
 
# Start the virtual machine
write-host “Starting the virtual machine “ $vmName “…”
$vmTask = $vm.Startup()
 
# Wait for the virtual machine to start
$vmTask.WaitForCompletion(-1)
 
# Display success or failure
If ($vmTask.result -eq 0) 
 {
 write-host “Virtual machine started.”
 }
Else
 {
 write-host “Failed to start virtual machine.”
 write-host
 write-host $vmTask.ErrorDescription
 }

 


VBScript:



Option Explicit
 
Dim namedArguments, vpc, vm, vmName, vmTask
 
‘ Check that the script is running at the command line.
If UCase(Right(Wscript.FullName, 11)) = “WSCRIPT.EXE” Then
 WScript.Echo “This script must be run under CScript.”
 WScript.Quit
End If
 
‘ Get the virtual machine name from the command-line arguments
Set namedArguments = WScript.Arguments.Named
 
If namedArguments.Exists(“vm”) Then
 vmName = namedArguments.Item(“vm”)
Else
 WScript.Echo “Missing command-line argument”
 WScript.Echo
 WScript.Echo “Usage: StartVM.vbs /vm:” & chr(34) & “Name of virtual machine to be started” & chr(34)
 WScript.Echo
 WScript.Quit
End If
 
‘ Attempt to connect to Virtual PC
On Error Resume Next
Set vpc = CreateObject(“VirtualPC.Application”)
If Err.Number <> 0 Then
 WScript.Echo “Unable to connect to Virtual PC.”
 WScript.Quit
End if
On Error Goto 0
 
‘ Get virtual machine object
Set vm = vpc.FindVirtualMachine(vmName)
 
‘ Start the virtual machine
WScript.Echo “Starting the virtual machine “ & vmName & “…”
Set vmTask = vm.Startup
 
‘ Wait for the virtual machine to start
vmTask.WaitForCompletion(-1)
 
WScript.Echo
 
‘ Display success or failure
If vmTask.result = 0 Then
 WScript.Echo “Virtual machine started.”
Else
 WScript.Echo “Failed to start virtual machine.”
 WScript.Echo
 WScript.Echo vmTask.ErrorDescription
End If
 
WScript.Echo

 


I have also attached these scripts to this post.


Cheers,
Ben

StartVM.zip