Skip to content

Copying your VM Disk to Storage account

  • by

To copy the VM disk to your storage account follow the below steps

Step 1: Create a New Storage account and a container inside in it

Step 2: Then open the Azure CLI

Step 3: Then execute the below script in AzureCLI

#Provide all the details as shown below
$subscriptionId = "3902ccd9-9777-4de2-a0a6-eefa61b34ca5"
$resourceGroupName ="yourResourceGroupName"
$managedDiskName = "yourManagedDiskName" 
$sasExpiryDuration = "3600"
$storageAccountName = "yourstorageaccountName"
$storageContainerName = "yourstoragecontainername"
$storageAccountKey = "yourStorageAccountKey"
$destinationVHDFileName = "yourvhdfilename"

#Logs in to the Azure Account & Select AzureRM subscription
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId $SubscriptionId

#Generate the SAS for the managed disk
$sas = Grant-AzureRmDiskAccess -ResourceGroupName $resourceGroupName -DiskName $managedDiskName -Access Read -DurationInSecond $sasExpiryDuration
$destinationContext = New-AzureStorageContext –StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey

#Copy the snapshot to the storage account and wait for it to complete
Start-AzureStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer $storageContainerName -DestContext $destinationContext -DestBlob $destinationVHDFileName
while(($state = Get-AzureStorageBlobCopyState -Context $destinationContext -Blob $destinationVHDFileName -Container $storageContainerName).Status -ne "Success") { $state; Start-Sleep -Seconds 5 }
$state 

This will copy the VM disk to VHD file (Virtual Hard Disk )

And the below command will upload the status as shown