2

如何使用 PowerShell 检查是否在 Azure 虚拟机上启用了备份?

我知道查询所有虚拟机,然后与在恢复服务保管库下配置的虚拟机进行比较。

有没有办法从虚拟机对象中提取信息?或者比我已经拥有的更简单的方法?

4

3 回答 3

3
#change the below values
$ResourceGroupName= "spfarm" #all the VMs in this Resource Group will be checked for Vault specified below
$VaultName = "vault595" 

Get-AzureRmRecoveryServicesVault -Name $VaultName -ResourceGroupName $ResourceGroupName | Set-AzureRmRecoveryServicesVaultContext
$fnames = Get-AzureRmRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered" | select  -ExpandProperty friendlyname
$vaultVMs=@()
foreach ($name in $fnames)
{
    $nameContainer = Get-AzureRmRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered" -FriendlyName $name
    $vaultVMs += Get-AzureRmRecoveryServicesBackupItem -Container $nameContainer -WorkloadType "AzureVM" | select VirtualMachineId
}

 $vms = Get-AzureRmVM -ResourceGroupName $ResourceGroupName
 foreach ($vm in $vms)
 {
    $j = 0;
    for ($i=0; $i -lt $vaultVMs.Count; $i++)
    {
        if ($vm.id -eq $vaultVMs[$i].VirtualMachineId)
        {
               $j++
        }
    }
    if ($j -eq 0)
    {
        Write-Host "$($vm.name) is not backed up in this vault"
    }
    else
    {
        Write-Host "$($vm.Name) is backed up in this vault"
    }
 }
于 2019-12-21T07:08:55.787 回答
0

使用 Get-AzureRmRecoveryServicesBackupContainer cmdlet 并将其通过管道传送到 Get-AzureRmRecoveryServicesBackupItem cmdlet,以列出恢复服务保管库下启用备份的 VM。

有关更多信息,请参阅:https ://docs.microsoft.com/en-us/azure/backup/backup-azure-vms-automation#select-the-vm

于 2018-04-25T13:37:41.827 回答
0

param (
    [Parameter(Mandatory=$true)][string]$tenantId
)

Connect-AzAccount -Tenant $tenantId
$subscriptions = @(Get-AzSubscription -TenantId $tenantId)

foreach ($subscription in $subscriptions)
{
    Set-AzContext -Subscription $subscription -Tenant $tenantId | Out-null

    $vaults = Get-AzRecoveryServicesVault
    [System.Collections.ArrayList]$vms = @(Get-AzVM)
    foreach ($vault in $vaults)
    {
        Set-AzRecoveryServicesVaultContext -Vault $vault
        $containers = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered"
        
        $vaultVMs=@()
        foreach ($container in $containers) # Fetch all backed-up VMs in the iterated vault
        {
            $vaultVMs += Get-AzRecoveryServicesBackupItem -Container $container -WorkloadType "AzureVM" | Select-Object VirtualMachineId
        }

        foreach ($vaultVM in $vaultVMs) # Remove the backed-up VMs from the array containing all the VMs in the iterated subscription
        {
            $indexsToRemove = @()
            for ($i=$vms.Count-1; $i -ge 0; $i--)
            {
                if ($vaultVM.VirtualMachineId -eq $vms[$i].id)
                {
                    $indexsToRemove += $i
                }
            }

            foreach ($indexToRemove in $indexsToRemove) {
                $vms.RemoveAt($indexToRemove)
            }
        }
    }

    if($vms.Count -gt 0) 
    {
        Write-Host "### SUBSCRIPTION $($subscription.Name) ###"
    }

    foreach ($vm in $vms) # Print out the VM names which are not backed up in the iterated subscription
    {
        Write-Host "$($vm.name) is not backed up"
    }

    if($vms.Count -gt 0) 
    {
        Write-Host ""
    }
}

我对Abhilash 的 anwswer中的脚本进行了一些改进,使其更加健壮,但也更适合我正在处理的用例。这些改进是:

  • 范围现在是特定租户下的所有订阅和所有资源组。此租户的 ID 是一个脚本参数。
  • 删除了一个多余的调用Get-AzureRmRecoveryServicesBackupContainer
  • $vms每当在包含所有 VM的阵列 ( ) 和包含在恢复库中备份的 VM 的阵列( )之间找到匹配项时$vaultVMs,都会更新第一个阵列,从而避免迭代时的冗余迭代$vms
于 2021-08-04T10:22:17.787 回答