MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.3k stars 21.48k forks source link

PowerShell script to get ASR replication health status for all servers in Azure Subscription. #66425

Closed patiljayesh040 closed 4 years ago

patiljayesh040 commented 4 years ago

Hello All,

I just need help with PowerShell script to get ASR replication status for all servers in Azure Subscriptions with Azure-to-Azure disaster recovery setup.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

MonikaReddy-MSFT commented 4 years ago

@patiljayesh040 - Thanks for brining this to our attention. We will investigate it further and update you shortly.

MonikaReddy-MSFT commented 4 years ago

@patiljayesh040 - This will be done using cmdlets in the Az.RecoveryServices module https://docs.microsoft.com/powershell/module/az.recoveryservices..

At this moment, Since there is no doc update required, We will now proceed to close this thread. If there are further questions regarding this matter, please tag me in your reply. We will gladly continue the discussion and we will reopen the issue.

thefabbius commented 4 months ago

Hope this might help:

`# Ensure you have installed and configured the Azure PowerShell module

Log in to Azure

Connect-AzAccount

Specify the subscription (optional if already connected to the correct subscription)

Set-AzContext -SubscriptionId ""

Specify the name of the Recovery Services Vault and the resource group

$resourceGroupName = "" $vaultName = ""

Get the Recovery Services Vault

$vault = Get-AzRecoveryServicesVault -ResourceGroupName $resourceGroupName -Name $vaultName

if ($vault -eq $null) { Write-Error "Vault not found. Please check the vault name and resource group." exit }

Set the context of the Recovery Services Vault

Set-AzRecoveryServicesVaultContext -Vault $vault Set-AzRecoveryServicesAsrVaultContext -Vault $vault

Get all fabrics associated with the Recovery Services Vault

try { $fabrics = Get-AzRecoveryServicesAsrFabric } catch { Write-Error "Error getting fabrics: $_" exit }

if ($fabrics.Count -eq 0) { Write-Error "No fabrics found." exit }

List of replicated items that require an update

$itemsToUpdate = @()

Iterate through each fabric to get the Protection Containers

foreach ($fabric in $fabrics) { try {

Get the Protection Containers for the specified fabric

    $protectionContainers = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric
} catch {
    Write-Error "Error getting Protection Containers for fabric $($fabric.FriendlyName): $_"
    continue
}

# Iterate through each Protection Container to get the replicated items
foreach ($container in $protectionContainers) {
    try {
        # Get the replicated items for the specified container
        $replicatedItems = Get-AzRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $container
    } catch {
        Write-Error "Error getting replicated items for container $($container.Name): $_"
        continue
    }

    # Check the Site Recovery Replication Agent status for each replicated item
    foreach ($item in $replicatedItems) {
        # Get provider-specific details
        $providerDetails = $item.ProviderSpecificDetails
        $vmName = $item.ReplicationProtectedItemFriendlyName

        # Extract the VM name from FabricObjectId if ReplicationProtectedItemFriendlyName is empty
        if ([string]::IsNullOrWhiteSpace($vmName)) {
            $vmName = ($providerDetails.FabricObjectId -split '/')[-1]
        }

        # Check if the replication agent update is required
        if ($providerDetails.IsReplicationAgentUpdateRequired -eq $true) {
            $itemsToUpdate += [PSCustomObject]@{
                VMName                      = $vmName
                AgentVersion                = $providerDetails.AgentVersion
                IsReplicationAgentUpdateRequired = $providerDetails.IsReplicationAgentUpdateRequired
                PrimaryFabricFriendlyName   = $item.PrimaryFabricFriendlyName
                RecoveryFabricFriendlyName  = $item.RecoveryFabricFriendlyName
            }
        }
    }
}

}

Path to the CSV file

$outputFile = ".\output.csv"

Export the replicated items that require an update to a CSV file

$itemsToUpdate | Export-Csv -Path $outputFile -NoTypeInformation

Confirmation message

Write-Output "Export completed. File saved to: $outputFile" `