I need to add some pre/post action to an Azure Site Recovery plan against the vm in the recovery plan. I have the following code, however, no VMs are listed. Please what can I do to enhance the code.
param (
[parameter(Mandatory=$true)]
[Object]$RecoveryPlanContext
)
$ErrorActionPreference = 'Continue'
$VerbosePreference = 'SilentlyContinue'
Disconnect-AzAccount -ErrorAction SilentlyContinue | Out-Null
Clear-AzContext -Scope Process -ErrorAction SilentlyContinue | Out-Null
Clear-AzContext -Scope CurrentUser -ErrorAction SilentlyContinue | Out-Null
Disable-AzContextAutosave –Scope Process | Out-Null
# Login to Azure
Connect-AzAccount -Identity
Write-Verbose "getting vmmap object"
$VMMapColl = $RecoveryPlanContextObj.VmMap
$VMCollection = @()
$VMinfo = $RecoveryPlanContext.VmMap | Get-Member | Where-Object MemberType -EQ NoteProperty | select -ExpandProperty Name
$vmMap = $RecoveryPlanContext.VmMap
foreach($VMID in $VMinfo)
{
$VMCollection += $VM
}
$CollectionCount = $VMCollection.Count
Write-Output "Collection Count: $CollectionCount"
#Returning Collection
$VMCollection.RoleName
Here is the updated
PowerShellscript to list all VMs in theAzure Site Recovery Plan.This
PowerShellscript will list all VMs in aService Planand then addpre/postactions to anAzure Site Recovery planas per your requirement.Output:
Reference: How do you get a list of all the VMs in a Recovery Plan with powershell