Script to perform a warm backup of a virtual machine (faster)

Here's an example of a script written in VBScript that you can use to make a warm backup of a virtual machine using UNC paths. It was written by John Kelbley. He says using this script that relies on UNC paths is faster than using the other script with the same title, so I added "faster" to the title of this script.

Please pay attention to all of the warnings and caveats in the script.

Thanks John!

Introduction from John

You may need to do some or all of the following to get UNC-based VHDs and the COM/DCOM stuff to work

Installation Config (stuff I did that I may not have needed to)

• Install Virtual Server for Constrained Delegation
• Set IIS - Virtual Server authentication to "Basic"
• Configure "Constrained Delegation"  as per the Virtual Server 2005 Administrator's Guide (Admin Guide)• Load hotfix listed in KB 829011 as per Admin Guide
• Add "Anonymous" to Everyone group on UNC endpoint (file server)
• Component Services / Computers / My Computer / Properties / Default Properties Tab - set Impersonation Level to Impersonate
• Component Services / Computers / My Computer / DCOM Config / Virtual Server / Properties / Security / Launch Permissions / Everyone - Allow

Setup

• Make sure that all the virtual network names you are going to use are the same on both (all) Virtual Servers
• Set all VHDs to use static MAC addresses
• Set search path / default config path in Virtual Servers to a UNC (give yourself lots of rights on the share and NTFS)
• Put VHDs out on share (no config files)
• Manually create config files on one of the Virtual Servers (need to be out on the UNC along with the VHDs)
• Fire up the VHDs
• Change Target Host in script to the name of the "TO" server
• Run script on the "FROM" server

And the script...

' READ THIS LINE!!!   The sample scripts are not supported under any Microsoft standard support program or service.  The sample scripts are provided AS IS without warranty of any kind.  Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose.  The entire risk arising out of the use or performance of the sample scripts and documentation remains with you.  In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
'
' Script saves the state of all running VMs in Virtual Server, register and start them on another system
' Assumes VMs are mounting via share (UNC)

' Cobbled together by John Kelbley
'
'
TargetHost  = "ToVirtServerName"  ' put name or IP Address of target server here, NOT preceeded by two slashes
TargetHostServer = "\\" & TargetHost

VMStateCodes = Array("Invalid", "Turned Off","Saved","Turning On", "Restoring", "Running", "Paused", "Saving",_
    "Turning Off", "Merging Drives", "Deleting Machine")

Dim VPCNames()
VPCCount = 0

Set vpcApp  = WScript.CreateObject("VirtualServer.Application")

' Get list of VMs on server
Set vmCollection = vpcApp.VirtualMachines

' Get maximum Name Length for Formatting
For each vm in vmCollection
 ReDim Preserve VPCName(VPCCount)
 VPCName(VPCCount) = vm.Name
 VPCCount = VPCCount + 1
 NameLength = Len(vm.Name)
 If MaxNameLength < NameLength Then
  MaxNameLength = NameLength
 End If
Next
Set vmCollection = Nothing
Set vpcApp = Nothing

Wscript.Echo VbCrLF & "Total VMs meeting migration criteria:  " & VPCCount & VbCrLF

For LoopCount = 0 to (VPCCount - 1)
 vm_name = VPCName(LoopCount)
 Set vpcApp = CreateObject("VirtualServer.Application")
 Set objVMUnregister = vpcApp.FindVirtualMachine(vm_name)

 Wscript.Echo VbCrLF & vm_Name & Space(MaxNameLength - Len(vm_name)) & " - " & VMStateCodes(objVMUnregister.State)
 While objVMUnregister.State > 2 ' if the VM is not turned off of saved...
  if VMStateCodes(objVMUnregister.State)  = "Running" Then
   objVMUnregister.Save()
 End If
  wscript.sleep(100)        
  Wscript.Echo Space(MaxNameLength) & "   " & VMStateCodes(objVMUnregister.State)
 Wend

 'Unregister on local box
 ConfigFile = objVMUnregister.File
 errReturn = vpcApp.UnregisterVirtualMachine(objVMUnregister)
 Set objVMUnregister = Nothing
 Set vpcApp = Nothing

 ' start call remote server directly

 Set objREMOTEVS = CreateObject("VirtualServer.Application", TargetHostServer)
 errReturn = objREMOTEVS.RegisterVirtualMachine("", ConfigFile)
 Set objREMOTEVM = objREMOTEVS.FindVirtualMachine(vm_Name)
 objREMOTEVM.Startup()
 Set objREMOTEVS = Nothing
 Set objREMOTEVM = Nothing

Next
Wscript.Echo VbCrLF & "Processing complete!" & VbCrLF