Script Sample - Azure Automation - Get VM Information from ASR Recovery Plan Context

@20aman    Sep 12, 2018

This script sample is to parse the Recovery Plan Context from Azure Site Recovery. When an Azure Automation Runbook is invoked directly from Azure Site Recovery, it passes an object called Recovery Plan Context. This has all the crucial information regarding the failover operation like:

  1. Recovery Plan Name
  2. Failover Type
  3. Failover DIrection
  4. Subscription Name
  5. Resource Group Name
  6. VM Name

Sample Recovery Plan Context

A sample Recovery Plan Context looks like below. Please note that this is formatted appropriately here. In the Runbook it will look flattened i.e. instead of multiple lines, the context will be contiguous and will be presented in a single long line. Its data type is Object when coming from ASR Recovery Plan.

{
   "RecoveryPlanName":"RecoveryPlan-Test",
   "FailoverType":"Test",
   "FailoverDirection":"PrimaryToSecondary",
   "GroupId":"Group1",
   "VmMap":{
      "aaaaaa-1111-1111-1111-aaaaaaaaaaa":{
         "SubscriptionId":"bbbbbbbb-2222-2222-2222-bbbbbbbbbbb",
         "ResourceGroupName":"rg-dr-test-asr",
         "CloudServiceName":null,
         "RoleName":"VMName-test",
         "RecoveryPointId":"cccccccccc-3333-3333-3333-cccccccccccc",
         "RecoveryPointTime":"\/Date(1550773609103)\/"
      }
   }
}

How does this Script work

The script first uses "ConvertFrom-Json" cmdlet to parses the object to valid PowerShell object. It then uses the VMMap property of the Recovery Plan Context object to get various pieces of the information.

The script also uses a foreach look to factor for multiple Vms in the Recovery Plan. It will output an array of the VMs with the required information, which can then be consumed in your logic to perform actual functions on the VMs. E.g. You can then connect to the VM and install some extensions on it.

How to Invoke This Runbook/Script

This script can be directly invoked from Azure Site Recovery (ASR)'s Recovery Plan or can be invoked from a master Runbook. It is a best practice to invoke this Runbook from a master Runbook. A sample for this Runbook is provided and discussed here: Script Sample - Azure Automation - Runbook for ASR Recovery Plan

Location of the Script

You can find this script in GitHub at this location: ASR-Get-VMSInfoFromASRContext.ps1





Comments powered by Disqus