Adding a VHD to Azure from PowerShell

Man-alive, this took me a few hours, there is nothing on the web that puts all of this together, so I’m writing it down for myself in the future and to maybe help someone else.

If you just need the answer to this, please skip down the very end for a plot twist!

My free Azure subscription via MSDN, via BizSpark is coming to an end in a couple of months. So, I’ve decided to provision exactly one VM in Azure and do all of my hosting from that. This means regular websites/ASP.NET apps, MySQL, MongoDB, and WordPress.

I did a dry run of everything I want to do – and it worked great. I’ll do another post on some lessons-learned. One last thing to make sure I have in place is a way to do backups.

So, I want create a separate Virtual Hard Drive (VHD), which I can access from within my virtual machine in Azure, to where I can create backups. This would be backups of: the web content, and of course the database backups. That way, if anything ever happens to this one server, I still have a live backup of all of the key data. I can also easily download the VHD and host the content elsewhere.

I know what you’re going to say: “Can’t you just go into the disks tab and create a new drive from there? Because that certainly is what the UI makes you think!” The answer of course is no. No, you can’t. Here’s what you gotta do:

Create a Storage Account:
A storage account is a general place where you can store “stuff”. In this case, we want to store a “page blob”, which will hold the contents of a virtual hard drive. You can do this by clicking on the big + in the bottom left of the Azure management portal. You choose Data Services –> Storage –> Quick Create


As a matter of convention, I saw that a lot of people name this with a “store” suffix. So, if your company name is Contoso, this might be contoso-vhd-store, for example.

Create an actual .vhd file with Hyper-V:
First, you might have noticed that on the main page of the storage account page, there is an option for “storage explorers”:


This is where I went first. I downloaded the Azure Storage Explorer – which is pretty cool! However, in this case, it doesn’t work the way you might think for virtual hard drive files.

From this interface, you can create and manage the blobs, queues, and tables – but there is a snag when you try to work with a VHD.


Now, if you click “New”, you’ll see that you can create a “page blob”, which is what a VHD needs to be:


Oh perfect! Now, back on Azure, surely I can point to it…right? Well, let’s see:


I click on the URL icon, point to my vhd blob:


I click OK and I get this in the console:


Oh, OK – so apparently I need an ACTUAL .vhd file. So, I have Hyper-V on my local machine, so I open that and over in the right-side menu, I create create a new, empty VHD:


I make a dynamic-sized disk and save it to my Desktop. We need to do some more things first, we’ll get back to this.

By the way, back in the Azure Storage Explorer app, you might see there is an “Upload” button. I tried uploading the .vhd, but then the Azure console says that a .VHD must be stored as a PageBlob – and by default it is stored as a BlockBlob. I found no way to change it, so this too was a dead-end.

Download and Install the Azure tools for PowerShell:
This is nice and easy – everything you need is on this page:

Once installed, in PowerShell (or ISE) you can see that the Azure module is now included:


Run the PowerShell commands:
Now, this is the hard part because the answer wasn’t all in one place. In fact, this took me about an hour to piece together just this part. First, here’s everything that you need to do this:

# When you run this, it will open a window, have you log in, 
# and prompt you to download the publishsettings for your Azure account. 

# Here are all of the pieces of information you need
$publishSettingsFile = "C:UsersMyNameDesktoppublish-lv.publishsettings"
$azureSubscriptionId = "D28159ED-2EA1-4789-9903-6DEAE358CED6"
$azureSubscriptionName = "Visual Studio Ultimate with MSDN"
$azureStorageAccountName = "MyStorageAccount" # Should match your actual "Storage Account" name
$azureStorageAccountUrl = ""
$localVhdFileName = "C:UsersMyNameDesktopvhdbackup.vhd"

# Load the settings file into memory
Import-AzurePublishSettingsFile -PublishSettingsFile $publishSettingsFile

# Set the subscription and storage account default
Set-AzureSubscription -SubscriptionId $azureSubscriptionId `
                      -SubscriptionName $azureSubscriptionName `
                      -CurrentStorageAccountName $azureStorageAccountName

# I was missing this and don't know why it's needed, but this "selects" the
# Azure subscription in some other way
Select-AzureSubscription -SubscriptionName $azureSubscriptionName

# Actually upload the VHD you created and put on your desktop,
# to your Storage Account up in Azure
Add-AzureVhd -Destination $azureStorageAccountUrl -Verbose -OverWrite `
             -LocalFilePath $localVhdFileName

.csharpcode, .csharpcode pre
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
background-color: #f4f4f4;
width: 100%;
margin: 0em;
.csharpcode .lnum { color: #606060; }


I don’t know what the difference is between Set-AzureSubscription and Select-AzureSubscription – but they are both needed. Without Select-AzureSubscription, I kept getting: Storage account ‘MyStorageAccount’ was not found. Assuming that was my storage account name.

Attaching the disk to the VM:
Now, once you have a VHD stored in your Storage Account, go to the Virtual Machine tab and you’ll see an Attach button:


When I clicked this, I literally laughed aloud:


Instead of doing all of this… you can just click “Attach empty disk”. Haha!

But if you did do it the long way like me, you’d choose “Attach disk” and you’ll see the disk you created in previous steps. It will reboot the VM, and when it comes back you will see it in “Disk Management” in the Computer Management app of your VM:


I create a volume and format it – and voila, I now have a disk drive in my VM where I can back things up to:


This means that I can also download this VM and bring it up on another machine too, if needed.

Posted in Azure, Cloud Computing, Computers and Internet, Infrastructure, New Technology, PowerShell, Uncategorized
2 comments on “Adding a VHD to Azure from PowerShell
  1. Nathan Work says:

    Great article! Here’s the difference between set-azuresubscription and select-azuresubscription:

    “The Set-AzureSubscription cmdlet configures common settings including subscription ID, management certificate, and custom endpoints. The settings are stored in a subscription data file in the user’s profile or in a user specified file. Multiple subscription data sets are supported and identified by a subscription name. To select a subscription and make it current, use the Select-AzureSubscription cmdlet.”


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Enter your email address to follow this blog and receive notifications of new posts by email.

Join 9 other followers

%d bloggers like this: