Virtual Server Scripts
I had the pleasure of demonstrating the scripting for Virtual Server 2005 recently for a group Academics in Mississauga, Ontario and as a result have had so many requests for the scripts I showed that I decided to post them here for you all! Now I can take some credit for these, but the meister is Ben Armstrong and so all Kudos to him! If you are interested in Virtual Server, I recommend having a look at his site" https://blogs.msdn.com/virtual_pc_guy/.
I will be making these scripts available over several posts, but they include:
Deployvm.vbs (a script to create a new virtual machine from a base image and deploy it in to a virtual server of your choice).
Backupvm.vbs (a variation on an earlier script that allows you to place a vm in saved state and back up it's relevant files)
Automation.vbs and Thumbnail.vbs (A couple of scripts used by Ben at MMS 2005 for demonstrating the automation capabilities)
StartVM.vbs, StopVM.vbs, SaveVM.vbs (a few short script that demonstrate automation of control of virtual machines)
And on with the show! Here is the script called DeployVM.vbs
There are 3 arguments passed to this script:
arguments(0) is the new virtual server name (and is used in the path)
arguments(1) is the base image for the differencing disk placed in the %vmPath%\base\ directory
arguments(2) is the name of the virtual server host. I have defaulted this to localhost in the script
There is a vmPath specifying the directory for the virtual machines you may wish to edit.
I have also included references for outputting the results of the creation to the screen or a text file.
--------------------------------------------------------------
'Start of Script
On Error Resume Next
'This script is used to deploy a virtual machine using differencing disks.
'Get virtual machine from command-line parameter
newvm = WScript.Arguments(0)
'Get virtual machine base image name from command-line parameter
basevm = WScript.Arguments(1)
'Get virtual server host name from command-line parameter
'vmhost = WScript.Arguments(2)
vmhost = "localhost"
'set virtual machine path
vmPath = "C:\images\"
' a couple of different methods of outputting the results
'.. to file
'If outputing to a txt file, set results file info... remember to close the file at the end.
'Const ForWriting = 2
'Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFile = objFSO.CreateTextFile(vmPath & "Results.txt") 'create file
'Set objFile = objFSO.OpenTextFile(vmPath & "Results.txt", ForWriting) 'open file
'Connect to Virtual Server
Set virtualServer = CreateObject("VirtualServer.Application", vmhost)
'Create new virtual machine
set vm = virtualServer.CreateVirtualMachine(newvm,vmPath & newvm)
'Create new differencing disk and wait for completion
set vmTask = virtualServer.CreateDifferencingVirtualHardDisk(vmPath & newvm & "\" & newvm & ".vhd", vmPath & "\base" & basevm)
vmTask.WaitForCompletion(-1)
'Create new virtual network
set virtualNetwork = virtualServer.CreateVirtualNetwork("Virtual Network " & newvm, vmPath & newvm)
'objFile.Writeline "New Virtual Network Created: Virtual Network " & newvm
Wscript.Echo "New Virtual Network Created: virtual network " & newvm
'Configure new virtual machine
vm.Memory = 256
set virtualNetwork = virtualServer.FindVirtualNetwork("Virtual Network " & newvm)
vm.NetworkAdapters.item(1).AttachToVirtualNetwork(virtualNetwork)
set vmHardDisk = vm.AddHardDiskConnection(vmPath & newvm & "\" & newvm & ".vhd",0,0,0)
'objFile.Writeline "New Virtual Machine Created: " & newvm
Wscript.Echo "New Virtual Machine Created: " & newvm
'Start Virtual Machine
'vm.Startup
'Start VMRC for demonstration purposes
Set objShell = CreateObject ("WScript.Shell")
objShell.Run("%windir%\system32\cmd /k" & chr(34) & chr(34) & "C:\Program Files\Microsoft Virtual Server\VMRC Client\vmrc.exe" & chr(34) &" "& chr(34) & "vmrc://127.0.0.1:5900/" & newvm & chr(34) & chr(34))
'close file if we are using a text file for output
'objFile.Close
'end of script