Virtual Server Scripts Part 2 - Backing up your virtual machines

More Virtual Server 2005 scripts...

Backupvm.vbs

Here is a nice script that is a variation on a version I posted a while ago. This script is designed to put a selected virtual machine in to saved state then back up the necessary files. There are 3 main arguments:

arguments(0) is vmName or the name of the virtual machine you want to back up (okay you probably guessed that one)

arguments(1) is vmPath and you guessed it is the path to the directory you wish to back up your virtual machine to.

arguments(2) is the hostname of the virtual server as you may have more than one :)

'script start

On Error Resume Next

'This script is designed to place your virtual machine in to saved state mode
'and to back up the files to a specified directory.
'There are 3 parameters: vmName, vmPath and vmHost

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

'Create Shell Object
Set objShell = CreateObject ("WScript.Shell")

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

'Set virtual machine from command-line parameter
set vm = virtualServer.FindVirtualMachine(vmName)

' 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

'objFile.Writeline "Virtual Machine Being Backed Up: " & vmName
'objFile.Writeline "to: " & vmPath

'..to the screen

Wscript.Echo "Backup up Virtual Machine: " & vmName
Wscript.Echo "To: " & vmPath

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

'Loop waiting for task completion
while not saveTask.isComplete
   WScript.Sleep 1000
wend

'Copy virtual hard disks and undo disks
for each vhd in vm.HardDiskConnections
   Wscript.Echo "Backing up file: " & vhd.undoHardDisk.file & " to path: " & vmPath
'   objFile.Writeline "Backing up file: " & vhd.undoHardDisk.file & " to path: " & vmPath
   objShell.Run "%comspec% /c copy " & chr(34) & vhd.undoHardDisk.file & chr(34) & " " & chr(34) & vmPath & chr(34),1 , True

   Wscript.Echo "Backing up file: " & vhd.HardDisk.file  & " to path: " & vmPath
'   objFile.Writeline "Backing up file: " & vhd.undoHardDisk.file & " to path: " & vmPath
   objShell.Run "%comspec% /c copy " & chr(34) & vhd.HardDisk.file & chr(34) & " " & chr(34) & vmPath & chr(34),1 , True
next

'Copy .VMC and .VSV files
objShell.Run "%comspec% /c copy " & chr(34) & vm.File & chr(34) & " " & chr(34) & vmPath & chr(34),1 , True
objShell.Run "%comspec% /c copy " & chr(34) & vm.SavedStateFilePath & chr(34) & " " & chr(34) & vmPath & chr(34),1 , True

'Once everything is done - startup the virtual machine
vm.Startup

'output success
Wscript.Echo "Machine Backup Done for: " & vmName
'objFile.Writeline "Machine Backup Done for: " & vmName

'if using text files, make sure we close the text file
'objFile.Close

'script end