Clone an Azure VM with PowerShell – Part 2: Copy the data drives

This post continues my series about cloning an Azure virtual machine. The script I'm going to discuss today copies the VM data drives and assigns them to the cloned VM.

Have a look at the entire script first. I will discuss the crucial part below.

The first thing is the parameters that the script accepts. Since these are mostly the same as in the previous script, I'm not going to explain them in detail here. In fact, the only parameters we need are the Azure service name, the source VM name and the new VM name.

To get the data drive's data, I have to get the Azure VM first, which I store in the $vm variable. The container name is "vhds" in my case; it could be different for your VMs though. And the last line just initializes the array $Datablobs = @(), where I am going to store the data blobs.

The three lines above update the console and log file.

The $DataDisks variable stores information about data disks, which I get with the Get-AzureDataDisk cmdlet. The next line checks whether the variable is empty. If it is, it means that the VM doesn't have any data drives and thus there is no need to continue.

However, if there are data drives, For-EachObject loops through them to get the storage account name (by splitting the MediaLink) and the data disk's host field. Then I only save the first part, which represents the storage account name, into the $StorageAccountName variable. I'm using the storage account name and the Get-AzureStorageKey cmdlet to get the storage account key and save it to the $StorageAccountKey variable. With this, I can create the storage account context with the New-AzureStorageContext cmdlet.

Next, I begin copying the data blob. I repeat the procedure for each data drive I have stored in the $DataDisks variable, creating blobs that later will became the data drives for the new VM. The $Datablobs variable stores information about the created data blobs.

After creating all data blobs successfully, I wait 30 seconds just to be on the safe side. I noticed that sometimes this is good idea, since in some cases, Azure reports completion of an operation before it actually completes.

Then I loop though the data blobs I created in the previous step and turn each of them into an Azure data disk using the Add-AzureDisk cmdlet. After that, I assign this disk to the cloned Azure VM that I created with the script in my previous article.

However, before I can assign the disks, I need to get the first available logical unit number (LUN) for the new VM. For this, I first have to store all the data disk information into the $disk variable so that I can extract the LUN information and get the highest LUN number using the Measure cmdlet.

I then add 1 to this number and use this LUN number to assign the data drive to the VM using the Add-AzureDataDisk cmdlet. After that, I pipe the result to the Update-AzureVM cmdlet to refresh the VM configuration.

Cone data drives in Azure with PowerShell

Cone data drives in Azure with PowerShell

When the script completes, the new VM has the same data drives as the source VM. The only thing left is to run sysprep on the new VM. I'll explain this process in my next article.

Want to write for 4sysops? We are looking for new authors.

Read 4sysops without ads and for free by becoming a member!


Leave a reply

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


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account