Search code examples
azurepowershellbackupvirtualpolicy

Azure powershell list assigned backup policy to VM


How can I make a list of all VM's inside my subscriptions including the information of:

  • Backup Policy
  • Backup Schedule
  • Backup times
  • Backup Retention (daily, weekly, monthly)

So additional to the result of this Get-AzVM -Status

Thx

I can get data from vm's and data from recovery services vaults, but not together.


Solution

  • Azure powershell list assigned backup policy to VM

    The command Get-AzVM -Status does not show any information about the VM backup or the recovery service vault. The following are the values of the Get-AzVM -Status command.

    enter image description here

    To check all VMs that are backed up in the recovery service vault, along with their Backup Schedule, Backup Policy, and Backup Retention (daily, weekly, monthly),' details, you can make use of the script below.

        $vaults = Get-AzRecoveryServicesVault
        foreach ($vault in $vaults) {
            Write-Host "Vault Name: $($vault.Name)"
            
            # Get all protection policies in the vault
            $policies = Get-AzRecoveryServicesBackupProtectionPolicy -VaultId $vault.Id -BackupManagementType AzureVM -WorkloadType AzureVM
        
            $containers = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -VaultId $vault.Id
        
            foreach ($container in $containers) {
                $containerName = $container.FriendlyName
        
                Write-Host "   VM Name : $containerName"
        
                $backupItems = Get-AzRecoveryServicesBackupItem -Container $container -VaultId $vault.Id -WorkloadType "AzureVM"
        
                foreach ($item in $backupItems) {
                    $output = @{
                        "Vault Name" = $vault.Name
                        "Container Name" = $containerName
                        "Backup Item Name" = $item.Name
                        "Backup Policy Name" = $policies.Name
                        "RetentionPolicy Name" = $policies.RetentionPolicy
                        "SchedulePolicy Name" = $policies.SchedulePolicy
                    }
        
                    New-Object PSObject -Property $output | Format-Table -AutoSize
                }
            }
        }
    

    Result:

    enter image description here