Veeam Recovery Orchestrator scripts and snippets

Plan customization via scripts

Veeam Recovery Orchestrator allows scripts to be uploaded into Orchestrator, to be executed when the recovery plan is run. They can be leveraged to provide additional functionality and implement advanced scenarios such as support for recovery of additional workloads, reconfiguration of recovery environments, and so on.

WARNING
Every example in this page is provided “as is” and meant as a starting point for further customization and refinement.
Please carefully review each line to understand the scripts’ functionality and avoid executing them without comprehension.

Recovering Agent backups using the embedded VBR

The current release of VRO (7.2.1) does not support recovery of Agent backups using the embedded VBR yet, meaning in a DR scenario the production VBR needs to be recovered first.
A powershell script can replace the main step in the Recovery plan, performing the Instant Recovery to a specific host of the DR Recovery Location added to VRO. The script example assumes the VM only needs a single network configured, but can be modified to have more.

# Agent_InstantRecovery.ps1
Param(
   [Parameter(Mandatory=$true)]
   [string]$current_vm_name,

   [Parameter(Mandatory=$true)]
   [string]$server,

   [Parameter(Mandatory=$true)]
   [string]$prod_network
)

try {
    Write-Host "Agent to recover: $current_vm_name"
    Write-Host "Recovery Host is $server"
    Write-Host "Assigned Network: $prod_network"

    $backup = Get-VBRBackup
    $restorepoint = Get-VBRRestorePoint -Name $current_vm_name -Backup $backup
    $targetnetwork= Get-VBRViServerNetworkInfo -Server $server | Where-Object { $_.NetworkName -eq "$prod_network" }
    Write-Host Performing Instant Recovery..
    $result = Start-VBRViComputerInstantRecovery -RestorePoint $restorepoint -Server $server -PowerOnAfterRestoring:$true -TargetNetwork $targetnetwork
    Write-Host "Successfully recovered $current_vm_name"
}
catch {
   Write-Error "Failed to recover $current_vm_name"
   Write-Error $_.Exception.Message
   }

Add the script in the Plan Steps section within the Administration page. Edit the Custom Script, skip to Parameters and do the following:

  • Set these Common parameters - Critical Step: Yes - Retries: 0
  • Change the Execute Location to Orchestrator Server
  • Add a New Step Parameter with Name: current_vm_name and Default Value: %current_vm_name%
  • Add a New Step Parameter with Name: server and Default Value the default ESXi host that is used for recovery
  • Add a New Step Parameter with Name: prod_network and Default Value the default production network that the VM connects to

Save and exit Administration.
Create a new Restore plan for Veeam Agent Backups, adding the agent groups as usual. Under Steps remove the Restore VM and Check VM Heartbeat steps and add the newly created custom script.

The plan is ready to run.
After the successful recovery the new VMs in the plan are powered on and should be migrated manually to a production datastore.

Testing Agent backups using the embedded VBR

If the recovery plan is meant for testing the Agent backup in an isolated environment, this script example enables the antivirus scan.

# Agent_InstantRecoveryTest.ps1
Param(
   [Parameter(Mandatory=$true)]
   [string]$current_vm_name,

   [Parameter(Mandatory=$true)]
   [string]$server
)


try {
    Write-Host "Agent to recover: $current_vm_name"
    Write-Host "Recovery Host is $server"

    $backup = Get-VBRBackup
    $restorepoint = Get-VBRRestorePoint -Name $current_vm_name -Backup $backup
    Write-Host Performing Antivirus scan..
    # Start Instant Recovery with AV Scan 
    Start-VBRInstantRecovery -RestorePoint $restorepoint -EnableAntivirusScan -EnableEntireVolumeScan -VirusDetectionAction AbortRecovery -Server $server
    $result = Get-Content "C:\Users\Administrator\AppData\Temp\VeeamTextEditor\Antivirus scan log" -Tail 2 |select-object -First 1
    Write-Host  "$result" 
    if (-not ($result -match "Infected: 0")) {
        # Trigger an alarm message
        Write-Error "ALARM: Infected files detected! Check your system immediately."
        exit 1 
    }

    # Stop the Instant Recovery session
    $RecoverySessionId = Get-VBRInstantRecovery | Where-Object { $_.VMName -eq "$current_vm_name" } | Select-Object -ExpandProperty Id
    $RecoverySession = Get-VBRInstantRecovery -Id "$RecoverySessionId"
    Stop-VBRInstantRecovery -InstantRecovery $RecoverySession
}
catch {
   Write-Error "Error during scan of $current_vm_name"
   Write-Error $_.Exception.Message
   }

Add the script in the Plan Steps section within the Administration page. Edit the Custom Script, skip to Parameters and do the following:

  • Set these Common parameters - During DataLab Tests: Execute - Critical Step: Yes - Retries: 0 - Timeout: 18000 (5 hours, tune as needed)
  • Change the Execute Location to Orchestrator Server
  • Add a New Step Parameter with Name: current_vm_name and Default Value: %current_vm_name%
  • Add a New Step Parameter with Name: server and Default Value the default ESXi host that is used for recovery

Save and exit Administration.
Create a new Restore plan for Veeam Agent Backups, adding the agent groups as usual. Under Steps remove the Restore VM and Check VM Heartbeat steps and add the newly created custom script.

The test plan is ready.
It can be run via the Datalab test, so it doesn’t need to be reset and can also be scheduled.
During execution VM disks are scanned, then the VM is briefly recovered to the ESXi host without any network, then powered off.

Test any port from the Orchestrator Server

By default, the pre-defined steps in VRO for ping and port checks execute the script from the production VBR, and these steps are fixed and cannot be modified.
A custom script can be employed to perform the check of any port during the execution or testing of a restore or replica plan from the Orchestrator server.

Please note that because DataLab tests require static routes to reach the masquerade IPs, you will need to either run the test from a DataLab configured in the embedded VBR, or to add these routes manually (may be integrated in the scripts as well).

# TestAnyPort.ps1
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$true)]
    [string]$Server,

	[Parameter(Mandatory=$true)]
    [int]$Port
)

if ($Server.Contains(',')) {
	$Servers = $Server.Split(',')
	Write-Host "Several IP addresses found, script will check them all"
}
else {
	$Servers = $Server
}

[bool] $testResult = $false
foreach ($s in $Servers) {
	if (-not $testResult) {
		$tcpClient = New-Object System.Net.Sockets.TcpClient
		try
		{
			Write-Host("Trying connect to server '{0}' and port '{1}'" -f $s, $Port)
			$tcpClient.Connect($s, $Port)
			$testResult = $true
		}
		catch [System.Net.Sockets.SocketException]
		{
			Write-Warning("Could not connect to server '{0}' and port '{1}'. Details: {2}" -f $s, $Port, $_.Exception.Message)
		}
		finally
		{
			if ($tcpClient.Connected)
			{
				Write-Host("Successfully established a connection with the server '{0}' via the port '{1}'" -f $s, $Port)
				$tcpClient.Close()
			}
		}
	}
}

if (-not $testResult) {
	Write-Error "Failed to verify port number"	
}

Add the script in the Plan Steps section within the Administration page.
Edit the Custom Script, skip to Parameters and do the following:

  • Change the Execute Location to Orchestrator Server
  • Add a New Step Parameter with Name: Server Type: Text and Default Value: %target_vm_ip% (for replica VMs use current_vm options, so it will work for both failover and failback)
  • Add a New Step Parameter with Name: Port Type: Signed Integer and Default Value: 3389 (that’s Windows RDP but you can set any default port, since you can set it in every individual VM in the plan as needed)

Save and exit Administration.
Add the custom step to the required VMs and customize the port number as required.

References


Back to top

Copyright © 2019 - 2025 Solutions Architects, Veeam Software.
Please note that information provided in this guide is not produced or verified by Veeam R&D but is a result of community effort based on the field observations.