Script Sample - Azure Automation - Runbook for ASR Recovery Plan
@20aman Sep 05, 2018When you are automating Azure Site Recovery, you will be working with Azure Automation Runbooks. You will invoke these from within the Azure Site Recovery Plan. The input parameter to this Runbook will be a Recovery Plan Context, that will automatically be passed by the ASR Recovery Plan. This Recovery plan context will contain information like:
- Recovery Plan Name
- Failover Type
- Failover DIrection
- Subscription Name
- Resource Group Name
- VM Name
The Azure Automation Runbook should be able to receive this information and parse it.
Best Practice and this Script Sample
As a best practice, you should leverage the main Runbook to perform operational functions. E.g. If the automation that you want to trigger on Failover, requires connection to the company's internal network, then you would want it to run on a Hybrid worker (which will be connected to your internal network). As such, I recommend that the parsing of the recovery plan context should be done in another Runbook, which should be invoked by the main Runbook.
This sample PowerShell Runbook accepts the Recovery Plan context and passes it to the second Runbook. That Runbook may or may not run on a Hybrid worker.
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)\/"
}
}
}
Location of the Script
You can find this script in GitHub at this location: ASR-RunbookForRecoveryPlans.ps1
Runbook for Parsing the Recovery Plan Context
The Runbook for parsing the Recovery Plan Context is discussed here: Script Sample - Get VM's Information from ASR Context. The Runbook/script in that link is the one that is being invoked by the Runbook discussed in this blog post.