We have a very popular article titled “SOLVED: PowerShell Script To Create VM Snapshots In Azure“. However, it doesn’t address larger clients who have multiple subscriptions.

This article will give you script you need to create and delete snapshots for all Azure subscriptions and Resource Groups within a single tenant.


# Login to Azure
Connect-AzAccount

# Get all subscriptions
$subscriptions = Get-AzSubscription

foreach ($subscription in $subscriptions) {
    # Set the current subscription context
    Set-AzContext -SubscriptionId $subscription.Id

    # Get a list of all resource groups
    $resource_groups = (az group list --query '[].name' --output tsv) -split "`n"

    # Loop through each resource group
    foreach ($resource_group in $resource_groups) {
        Write-Host "Processing resource group: $resource_group"

        # Get a list of all virtual machines except those starting with "WHATEVA"
        # Change "WHATEVA" to what ever you want, IF you want to skip some VM's
        $vms = (az vm list --resource-group $resource_group --query "[? !starts_with(name, 'WHATEVA-')].[name]" --output tsv) -split "`n"

        # Loop through each virtual machine and create a snapshot of its managed disk
        foreach ($vm in $vms) {
            Write-Host "Creating snapshot of VM: $vm"
            $disk_name = (az vm show -d -g $resource_group -n $vm --query "storageProfile.osDisk.name" -o tsv) -split "`n"
            $snapshot_name = "URTech-SNAP-$vm-$(Get-Date -Format 'yyyyMMddHHmmss')"
            az snapshot create --resource-group $resource_group --source $disk_name --name $snapshot_name
        }
    }
}

Here is a simplified version of that same script to create snapshots in Azure for all resource groups and all subscriptions you have access to:


Connect-AzAccount
Get-AzSubscription | ForEach-Object {
    Set-AzContext -SubscriptionId $_.Id
    $resource_groups = (az group list --query '[].name' --output tsv) -split "`n"
    foreach ($resource_group in $resource_groups) {
        Write-Host "Processing resource group: $resource_group"
        $vms = (az vm list --resource-group $resource_group --query "[? !starts_with(name, 'WHATEVA-')].[name]" --output tsv) -split "`n"
        foreach ($vm in $vms) {
            Write-Host "Creating snapshot of VM: $vm"
            $disk_name = (az vm show -d -g $resource_group -n $vm --query "storageProfile.osDisk.name" -o tsv) -split "`n"
            $snapshot_name = "URTech-SNAP-$vm-$(Get-Date -Format 'yyyyMMddHHmmss')"
            az snapshot create --resource-group $resource_group --source $disk_name --name $snapshot_name
        }
    }
}

After your work is done and you want to delete those snaps, you can use this command script:


Connect-AzAccount
Get-AzSubscription | ForEach-Object {
    Set-AzContext -SubscriptionId $_.Id
    Get-AzSnapshot -Name "URTech-SNAP-*" | Remove-AzSnapshot -Force
}


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *