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

时间:2018-04-25 13:10:07

标签: azure azure-powershell azure-backup-vault

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

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

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

3 个答案:

答案 0 :(得分:1)

#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"
    }
 }

答案 1 :(得分:0)

使用Get-AzureRmRecoveryServicesBackupContainer cmdlet并将其传递到Get-AzureRmRecoveryServicesBackupItem cmdlet,以列出恢复服务库下的已启用备份的VM。

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

答案 2 :(得分: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's anwswer 中的脚本进行了一些改进,使其更加健壮,但也更适合我正在处理的用例。这些改进是:

  • 范围现在是特定租户下的所有订阅和所有资源组。此租户的 ID 是一个脚本参数。
  • 删除了对 Get-AzureRmRecoveryServicesBackupContainer 的多余调用
  • 只要在包含所有 VM 的阵列 ($vms) 与包含在恢复保管库中备份的 VM 的阵列 ($vaultVMs) 之间找到匹配项,就会更新第一个阵列,从而在迭代 $vms
  • 时避免冗余迭代