We have several articles on how to create snapshot for various Azure scenarios including:

It used to be that take it a snapshot of a virtual machine’s hard disks via the azure gui caused the virtual machines to power down for just a moment and then power back up as soon as the snap was complete. We haven’t seen that in the last year, but it is still a pain to take the snapshots manually especially if the VM in question has multiple disks.

We use this simple script to via the Cloud Shell to snapshot a single Azure VM:

# Set the Context 
# Can omit first 2 lines if you only have 1 subscription
$subscriptionName = "Name-of-Azure-Subscription"
Set-AzContext -Subscription $subscriptionName
$vmName = "Name-of-VM"
$resourceGroupName = "Name-of-RG"

# Get the VM object
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name $vmName

# Function to create a snapshot
function Create-Snapshot {
    param (
        [string]$diskId,
        [string]$snapshotName,
        [string]$location
    )

    $snapshotConfig = New-AzSnapshotConfig -SourceUri $diskId -Location $location -CreateOption Copy
    New-AzSnapshot -Snapshot $snapshotConfig -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
}

# Create snapshots for OS and data disks
$vm.StorageProfile.OsDisk, $vm.StorageProfile.DataDisks | ForEach-Object {
    $diskId = $_.ManagedDisk.Id
    $snapshotName = if ($_.Lun -ne $null) { "URTech-SNAP-$vmName-DataDisk-$($_.Lun)-Snapshot" } else { "URTech-SNAP-$vmName-OSDisk-Snapshot" }
    Create-Snapshot -diskId $diskId -snapshotName $snapshotName -location $vm.Location
}

After this has completed you should see the snapshots of all disks in Azure SNAPSHOTS.

You can then use Get-AzSnapshot | Remove-AzSnapshot -Force to delete all snapshots when you no longer need them


0 Comments

Leave a Reply

Avatar placeholder

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