When experimenting in my lab environment I want to create a backup of the virtual machines. The following PowerShell script will do just that. I assume you’ve already setup your PowerShell to work with azure by doing the following:

  1. Setup the Azure PowerShell cmdlets (see: http://azure.microsoft.com/en-us/downloads/)
  2. imported your Publish Settings File (see Get-AzurePublishSettingsFile and Import-AzurePublishSettingsFile)
  3. Defined which storageaccount to use with Set-AzureStorageAccount
  4. Shutdown all the Virtual Machines
Import-Module Azure -ErrorAction Stop
$backupContainerName = "backups"
function Backup-Lab
{
    $vms = Get-AzureVM

    if (! (Get-AzureStorageContainer -Name  $backupContainerName -ErrorAction SilentlyContinue) )
    {
        New-AzureStorageContainer -Name $backupContainerName -Permission Off
    }

    foreach ($vm in $vms)
    {
        Write-Host "backing up machine: " $vm.Name
        $disks = @()
        $disks +=  $vm | Get-AzureOSDisk
        $disks +=  $vm | Get-AzureDataDisk

        foreach($disk in $disks)
        {
            $DiskBlobName = $disk.MediaLink.Segments[-1]
            $DiskContainerName = $disk.MediaLink.Segments[-2].Split('/')[0]
            Write-Host "disk: " $disk.DiskName
            #Start an asynchronous copy of the VHD to our backup destination
            Start-AzureStorageBlobCopy -SrcContainer $DiskContainerName -SrcBlob $DiskBlobName -DestContainer $backupContainerName -DestBlob $DiskBlobName
            #Wait for the copy to complete
            Get-AzureStorageBlobCopyState -Blob $DiskBlobName -Container $DiskContainerName -WaitForComplete
        }
    }