我正在为客户编写脚本,似乎 ASR PowerShell 模块无法正常工作。
我需要通过 PowerShell 连接到 Azure,并获取受保护项的列表。(复制项(。
有人尝试过这样做吗?我尝试使用Az和AzureRM模块。
請知道,所有版本的 AzureRMPowerShell 模組都已過時,但並未失去支持。AzPowerShell 模块现在是推荐的用于与 Azure 交互的 PowerShell 模块。
不过,下面是来自这两个模块的 cmdlet:
- AzureRM module: Get-AzureRmSiteRecoveryReplicationProtectedItem
Get-AzureRmSiteRecoveryReplicationProtectedItem
-ProtectionContainer <ASRProtectionContainer>
[-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
Az- Module: Get-AzRecoveryServicesAsrReplicationProtectedItem
Get-AzRecoveryServicesAsrReplicationProtectedItem
-ProtectionContainer <ASRProtectionContainer>
[-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
首先授权给 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
这是我所做的。我必须做多个循环才能获得所有信息 😅
脚本支持多个订阅和恢复服务保管库。 我还在 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