Skip to content

Instantly share code, notes, and snippets.

@transiient
Created October 6, 2022 08:21
Show Gist options
  • Save transiient/48abd5eb411e1299263da1ee3b0d4e38 to your computer and use it in GitHub Desktop.
Save transiient/48abd5eb411e1299263da1ee3b0d4e38 to your computer and use it in GitHub Desktop.
Locate Azure VMs that aren't being backed up
# Find VMs which aren't backed up by Azure
#
# Sam Crossman (https://github.com/sam-cross)
# You'll need the Az PowerShell module for this :)
# Set subscription via Set-AzContext before running this script
$SubscriptionName = (Get-AzContext).Subscription.Name
# Enumerate all VMs in all backup vaults within the subscription
$VMIDs_AllVaults = @()
foreach ($VaultResource in (Get-AzResource -ResourceType "Microsoft.RecoveryServices/vaults")) {
Write-Output "Searching Vault $($VaultResource.Name)..."
Get-AzRecoveryServicesVault -Name $VaultResource.Name -ResourceGroupName $VaultResource.ResourceGroupName | Set-AzRecoveryServicesVaultContext
$VaultContainers = Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered"
Foreach ($VaultContainer in $VaultContainers) {
$VMIDs_AllVaults += Get-AzRecoveryServicesBackupItem -Container $VaultContainer -WorkloadType "AzureVM" | Select-Object VirtualMachineId
}
}
# Get all VMs in subscription - don't filter by RSG
$VMs = Get-AzVM
# Log every VM which is not found in All Vaults
Foreach ($VMResource in $VMs) {
$j = 0;
For ($i=0; $i -lt $VMIDs_AllVaults.Count; $i++) {
If ($VMResource.Id -eq $VMIDs_AllVaults[$i].VirtualMachineId) { $j++ }
}
If ($j -eq 0) {
Write-Host "Virtual Machine $($VMResource.Name) is NOT currently in a backup vault"
Write-Output "$($VMResource.Name)" >> "C:\Temp\AzureVMsNotBackedUp-$($SubscriptionName).txt"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment