# 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
3条答案
按热度按时间r7s23pms1#
请注意,AzureRMPowerShell模块的所有版本都已过时,但并未停止支持。AzPowerShell模块现在是用于与Azure交互的推荐PowerShell模块。
不过,以下是来自这两个模块的cmdlet:
30byixjq2#
首先授权到Azure,然后执行以下操作:
o8x7eapl3#
这就是我所做的。我得做多个循环才能得到所有信息😅
脚本支持多个订阅和恢复服务Vault。我还查询VM上的TAG。输出到CSV文件。