-- Ben Armstrong, Virtualization Program Manager
Talking about core virtualization at Microsoft (Hyper-V, Virtual PC and Virtual Server).
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:
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")
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)
' 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."
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)
' Display success or failure
If vmTask.result = 0 Then
WScript.Echo "Virtual machine started."
WScript.Echo "Failed to start virtual machine."
WScript.Echo vmTask.ErrorDescription
I have also attached these scripts to this post.
Cheers, Ben
Hi Ben,
Is it possible to also configure a log off script to shut down a running VM?
I use Windows on my media center and then have my email server running in a VM on the same box. Virtual Server lets you start/stop the VM with the service, is there a way I could mirror this functionality in Win7 using Virtual PC?
Cheers,
James
Yes, you can script shutting down a virtual machine, but getting it to run on logoff would be tricky.
Ben
Is there any possibility to turnoff a hibernated windows virtual pc machine? When trying your code and executing the turnoff method instead of the startup methods it results in an error stating that the maschine is not running.