PS script to validate any failure on SRM

To validate any failures on VMware Site Recovery Manager (SRM), you can use PowerShell along with VMware PowerCLI to check the status of the recovery plans, protection groups, and the overall SRM environment. Here’s a PowerShell script that helps you validate SRM failures:

# VMware PowerCLI Module Import
Import-Module VMware.PowerCLI

# Connect to vCenter Servers
$protectedSiteServer = "Protected_Site_vCenter_Server"
$recoverySiteServer = "Recovery_Site_vCenter_Server"

$protectedSiteCredential = Get-Credential -Message "Enter the credentials for the Protected Site vCenter Server"
$recoverySiteCredential = Get-Credential -Message "Enter the credentials for the Recovery Site vCenter Server"

Connect-VIServer -Server $protectedSiteServer -Credential $protectedSiteCredential -ErrorAction Stop
Connect-VIServer -Server $recoverySiteServer -Credential $recoverySiteCredential -ErrorAction Stop

# Function to Check Recovery Plan Status
function Get-RecoveryPlanStatus {
    param (
        [Parameter(Mandatory=$true)]
        [string]$RecoveryPlanName
    )
    $recoveryPlan = Get-SRRecoveryPlan -Name $RecoveryPlanName -ErrorAction SilentlyContinue
    if ($recoveryPlan) {
        $planStatus = $recoveryPlan.ExtensionData.GetStatus()
        Write-Host "Recovery Plan: $($recoveryPlan.Name)"
        Write-Host "Status: $($planStatus.State)"
        Write-Host "Protection Status: $($planStatus.ProtectionStatus)"
        Write-Host "Recovery Status: $($planStatus.RecoveryStatus)"
        Write-Host ""
    } else {
        Write-Host "Recovery Plan '$RecoveryPlanName' not found."
    }
}

# Function to Check Protection Group Status
function Get-ProtectionGroupStatus {
    param (
        [Parameter(Mandatory=$true)]
        [string]$ProtectionGroupName
    )
    $protectionGroup = Get-SRProtectionGroup -Name $ProtectionGroupName -ErrorAction SilentlyContinue
    if ($protectionGroup) {
        $groupStatus = $protectionGroup.ExtensionData.GetStatus()
        Write-Host "Protection Group: $($protectionGroup.Name)"
        Write-Host "Status: $($groupStatus.State)"
        Write-Host "Number of Protected VMs: $($groupStatus.NumberOfProtectedVms)"
        Write-Host "Number of Recovered VMs: $($groupStatus.NumberOfRecoveredVms)"
        Write-Host ""
    } else {
        Write-Host "Protection Group '$ProtectionGroupName' not found."
    }
}

# Main Script

# Get Recovery Plans and Protection Groups
$recoveryPlans = Get-SRRecoveryPlan
$protectionGroups = Get-SRProtectionGroup

# Check Recovery Plan Status
Write-Host "Checking Recovery Plan Status..."
foreach ($recoveryPlan in $recoveryPlans) {
    Get-RecoveryPlanStatus -RecoveryPlanName $recoveryPlan.Name
}

# Check Protection Group Status
Write-Host "Checking Protection Group Status..."
foreach ($protectionGroup in $protectionGroups) {
    Get-ProtectionGroupStatus -ProtectionGroupName $protectionGroup.Name
}

# Disconnect from vCenter Servers
Disconnect-VIServer -Server $protectedSiteServer -Confirm:$false
Disconnect-VIServer -Server $recoverySiteServer -Confirm:$false

Save the above script in a .ps1 file and run it with PowerShell. The script will prompt you to enter the credentials for the Protected Site vCenter Server and the Recovery Site vCenter Server. It will then connect to both vCenter Servers, retrieve the status of all the recovery plans and protection groups in the SRM environment, and display the results in the PowerShell console.

The script will check the status of recovery plans and protection groups, including their state, protection status, recovery status, number of protected VMs, and number of recovered VMs. It will also handle cases where a recovery plan or protection group is not found in the SRM environment.

This script helps you quickly validate any failures in your SRM environment and can be scheduled to run periodically for proactive monitoring. You can also integrate it into your monitoring systems to receive alerts in case of any issues with SRM recovery plans and protection groups.

Leave a comment