如何从Azure Site Recover中获取所有受保护的项目及其详细信息

wvyml7n5  于 2023-05-29  发布在  其他
关注(0)|答案(3)|浏览(191)

我正在为我的客户编写一个脚本,似乎ASR PowerShell模块不工作。
我需要通过PowerShell连接到Azure并获取受保护项目的列表。(复制项)。
有人试过吗?我尝试了AZ和AzureRM模块。

r7s23pms

r7s23pms1#

请注意,AzureRMPowerShell模块的所有版本都已过时,但并未停止支持。AzPowerShell模块现在是用于与Azure交互的推荐PowerShell模块。
不过,以下是来自这两个模块的cmdlet:

Get-AzureRmSiteRecoveryReplicationProtectedItem
   -ProtectionContainer <ASRProtectionContainer>
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]
Get-AzRecoveryServicesAsrReplicationProtectedItem
   -ProtectionContainer <ASRProtectionContainer>
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]
30byixjq

30byixjq2#

首先授权到Azure,然后执行以下操作:

$VaultName = 'name-of-recovery-service-vault'
$vault = Get-AzRecoveryServicesVault -Name $VaultName
Set-AzRecoveryServicesAsrVaultContext -Vault $vault
$fabric = Get-AzRecoveryServicesAsrFabric
$container = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric
Get-AzRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $container
o8x7eapl

o8x7eapl3#

这就是我所做的。我得做多个循环才能得到所有信息😅
脚本支持多个订阅和恢复服务Vault。我还查询VM上的TAG。输出到CSV文件。

# Connect to Azure
Connect-AzAccount

# Get all Azure subscriptions
$subscriptions = Get-AzSubscription

# Create an array to store the results
$results = @()

# Loop through each subscription
foreach ($subscription in $subscriptions) {
    
    # Select the current subscription
    Set-AzContext -SubscriptionId $subscription.Id

# Get all vaults
$vaults = Get-AzRecoveryServicesVault

# Loop through each vault
foreach ($vault in $vaults) {
    
    # Select the current vault
    Set-AzRecoveryServicesAsrVaultContext -Vault $vault

# Get all fabrics
$fabrics = Get-AzRecoveryServicesAsrFabric

# Loop through each fabric
foreach ($fabric in $fabrics) {
    
    # Get the asr container
    $container = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric
    
    # Get protected Items
    $items= Get-AzRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $container

# Loop through each item
foreach ($item in $items) {

        # Check if replication is enabled for the virtual machine
        if ($item -ne $null) {
        
        # Get VM object to be able to populate with VM parameters
        $vm = get-azvm | Where-Object { $_.Name -match $item.FriendlyName}
        
            $result = [PSCustomObject]@{
                "Subscription" = $subscription.Name
                "RecoveryVault" = $vault.Name
                "Fabric" = $fabric.Name
                "VirtualMachineName" = $item.FriendlyName
                "AppName" = $vm.Tags.AppName
                "ActiveLocation" = $item.ActiveLocation
                "ProtectionState" = $item.ProtectionState
                "ReplicationHealth" = $item.ReplicationHealth

            } 
            $results += $result
            } #end if

} #end VM loop

} #end fabric loop

} #end vault loop

} #end sub loop

# Output the results to a CSV file
$results | Export-Csv -Path "VirtualMachineReplicationStatus.csv" -NoTypeInformation

相关问题