Managing VPGs : Running Scripts Before or After Recovering a VPG
  
Running Scripts Before or After Recovering a VPG
Before and after executing a failover, move, or test failover, you can run executable scripts, such as Windows .bat files or PowerShell scripts. A pre-recovery script is always run at the beginning of the recovery operation. A post-recovery script is run after all the virtual machines are powered on at the recovery site.
The scripts must be saved to the machine where the remote Zerto Virtual Manager is installed.
Note: Zerto recommends duplicating scripts on the Zerto Virtual Managers for both the protected and recovery sites, so that if reverse replication is required, the scripts are available. The location of the script for reverse replication, on the machine where the Zerto Virtual Manager that manages the protected site is installed, must be to the same path as in the remote Zerto Virtual Manager machine. For example, if the scripts are saved to C:\ZertScripts on the remote Zerto Virtual Manager machine, they must be saved to C:\ZertScripts on the local Zerto Virtual Manager machine.
The scripts can include environment variables that can be included as part of the script itself, or passed to the script as parameters. When the script is passed an environment variable as a parameter, the variable is evaluated before executing the script. The following environment variables are available:
%ZertoVPGName% – The name of the VPG. If the name includes a space, enclose the variable in double quotes (”). For example, the VPG MyVPG uses the format %ZertoVPGName% but the VPG My VPG uses the format “%ZertoVPGName%”.
%ZertoOperation% – The operation being run: FailoverBeforeCommit, FailoverRollback, Test, MoveBeforeCommit, MoveRollback. Use the result returned for this variable to limit when the script runs, dependent on the operation. The scripts are run after all the virtual machines are powered on at the recovery site and the variable is set to FailoverBeforeCommit or MoveBeforeCommit. Use FailoverRollback or MoveRollback when rolling back the Failover or Move operation, to undo whatever changes a previous script has done (such as updating the DNS records).
%ZertoVCenterIP% – The IP address of the hypervisor manager, VMware vCenter Server or Microsoft SCVMM, where the VPG is recovered.
%ZertoVCenterPort% – The port used by the Zerto Virtual Manager to communicate with the hypervisor manager, VMware vCenter Server or Microsoft SCVMM.
%ZertoForce% – A Boolean value, Yes/No, that dictates whether to abort the recovery operation if the script fails. For example, whether to rollback a Move operation when the script fails and returns a non-zero value.
For example, if a specific VPG should not be migrated, the pre-recovery script can determine whether to continue based on the values of the %ZertoOperation% and %ZertoVPGName%.
When specifying scripts in the definition of a VPG, enter values for the Pre-recovery Script and Post-recovery Script:
Command to run – The full path of the script to run. The script must be located on the same machine as the Zerto Virtual Manager for the recovery site.
Params – The values of any parameters to pass to the script. Separate parameters with a space.
Timeout (sec) – The time-out in seconds for the script to run. If the script runs before executing a failover, move, or test failover and the script fails or a timeout value is reached, an alert is generated and the failover, move, or test failover is not performed. If the script runs after executing a failover, move, or test failover and the timeout value is reached, an alert is generated. The default timeout value is specified in the Site Configuration Advanced Settings dialog.
Creating a Script
There are many ways to create scripts to run before or after recovering a VPG. The following procedure uses a Windows PowerShell file (.ps1) or a batch (.bat) file.
To create a script:
1. Create a file on the machine where the Zerto Virtual Manager that manages the recovery is installed.
2. Enter the script that you want to run in the file.
3. Save the file as a Windows PowerShell file (.ps1) or batch (.bat) file.
When writing a PowerShell script, you can include the environment variables in the script. For example, the following code snippet shows the use of the %ZertoOperation% and %ZertoVPGName% environment variables:
$Operation = $env:ZertoOperation
$VPG = $env:ZertoVPGName
$time = Get-Date
 
if ($Operation -eq "Test") {
  "$time VPG: $VPG was tested." >> "C:\ZertoScripts\VPG_DR.txt"
}
 
if ($Operation -eq "FailoverBeforeCommit") {
  "$time Failover before commit was performed. VPG: $VPG" >> "C:\ZertoScripts\VPG_DR.txt"
}
 
if ($Operation -eq "MoveBeforeCommit"){
  "$time Move before commit was performed. VPG: $VPG" >> "C:\ZertoScripts\VPG_DR.txt"
}
Pre-recovery scripts must be saved on the protected site Zerto Virtual Manager machine. Post-recovery scripts must be saved on the recovery site Zerto Virtual Manager machine.
Note: Zerto recommends having both pre- and post-recovery scripts, available on both the protected and recovery Zerto Virtual Manager machines, so that they will work from the protected site and after reverse protection from the recovery site.
4. Update Command to run and Params fields for all the VPG definitions that you want to run the script.
Passing parameters is implemented differently for the two script types. For information about passing command line parameters, refer to the relevant PowerShell or batch file documentation.
Using a BAT File
Windows Batch (.bat) is an executable file that does not require anything in order to run. Update Command to run and Params fields for all the VPG definitions that you want to run the script.
Command to run<script_including_path>
C:\ZertoScripts\PostScript.bat
Use quotes (“) around the path if it includes spaces. The bat file is an executable file and is therefore included in the Command to run field.
Params <Zerto_Params>, for example:
%ZertoOperation% %ZertoVPGName%
Using a PowerShell Script
Windows PowerShell scripts require Windows PowerShell (.exe) to execute. To specify a PowerShell script, update Command to run and Params fields for all the VPG definitions that you want to run the script.
Command to runpowershell.exe
Params <script_including_path> <Zerto_Params>, for example:
C:\ZertoScripts\PostScript.ps1 %ZertoOperation% %ZertoVPGName%
Use quotes (“) around the path if it includes spaces.
Note: You might have to set the remote signed execution policy. For example, using the following:
##PowerCLI requires remote signed execution policy - if this is not enabled,
##it may be enabled here by uncommenting the line below.
 
##Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
Note: Zerto recommends testing both PowerShell and batch scripts by running them from the command line, to ensure that they run correctly.
Example Scripts
The following scripts are examples of how to provide scripts to use with Zerto Virtual Replication:
Example 1 – Recording Failover Tests.
Example 2 – Moving Virtual Machines to a Resource Pool After a Failover.
Example 1 – Recording Failover Tests
The following script, c:\ZertoScripts\TestedVPGs.bat, writes the VPG name and date to the ListOfTestedVPGs.txt file every time a failover test is run:
SET isodt=%date:~10,4%-%date:~7,2%-%date:~4,2% %time:~0,2%-%time:~3,2%-%time:~6,2%
IF %1==Test ECHO %2 %isodt% >> c:\ZertoScripts\Results\TestedVPGs.txt
Where %1 is the first parameter in the list of parameters, %ZertoOperation%, and %2 is the second parameter in the list of parameters, %ZertoVPGName%.
Note: If the file TestedVPGs.txt does not exist it is created, as long as the folder, c:\ZertoScripts\Results, exists.
Example 2 – Moving Virtual Machines to a Resource Pool After a Failover
The following PowerShell script is an example of how to move virtual machines into resource pools as a post-recovery script. This script could be used when you want to move virtual machines into a resource pool following a failover and want to designate the resource pool only at the time of the failover and not as part of the VPG definition. Note that this script is a basic example and requires some configuration, as noted in the comments of the script:
##The following are a list of requirements for this script:
##    - This script must be present in the same directory on both sites listed in
##      the Manage VPGs dialog
##    - PowerShell v2.0 installed on both Zerto Virtual Managers
##    - VMWare PowerCLI installed on both Zerto Virtual Managers
##
##This script was written by Zerto Support and is used at the customer's own risk
## and discretion.
##
##Note: The desired resource pool MUST exist on the hypervisor manager prior to
##running this script.
##
##To run this script from the VPG screen, an example command is 'powershell.exe'
##with the parameter 'C:\ZertoScripts\Move-VMs.ps1'
##
##START OF SCRIPT
##
##PowerCLI requires remote signed execution policy - if this is not enabled,
##it may be enabled here by uncommenting the line below.
 
##Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force
 
##Below are the variables that must be configured.
##Variables:
 
##The location of this script
$strMoveScriptLoc = "C:\Zerto Scripts\"
 
##vCenter IP address
$strVCenterIP = "10.10.10.10"
 
##vCenter user account to use; account must have ability to move desired machines
$strVCUser = "Administrator"
##vcenter user password
$strVCPw = "password"
 
##Name of resource pool in vCenter
$strResPool = "ResourcePool"
 
##Array of VMs to move; it includes ALL VMs in the VPG and is case sensitive.
$strVMtoMove = @("VM-1", "VM-2", "VM-3")
 
##The PowerCLI snap-in must first be registered
Add-PSSnapin VMware.VimAutomation.Core
 
##Move to directory where script is located
CD $strMoveScriptLoc
 
##Connect to target VC server based on variables above
Connect-VIServer -Server $strVCenterIP -Protocol https -User $strVCUser -Password $strVCPw
 
##execute the move for each VM specified
foreach ($objVM in $strVMtoMove){
  Move-VM -VM $objVM -Destination $strResPool }
 
##Disconnect from session with VC server
Disconnect-VIServer -Server $strVCenterIP -Force
 
##End of script