Migrate a Virtual Machine to Windows Azure with PowerShell

In my previous post I show how you can use the Add-AzureVHD cmdlet to upload a VHD. I wanted to take it a bit further and show how you can use this new cmdlet in conjunction with the other PowerShell cmdlets to migrate and provision an entire virtual machine.

The script below uploads two VHDs; one for the Operating System Disk and one for an additional data disk. Once the VHDs are uploaded it then creates the disk entities using the Add-AzureDisk cmdlet and then proceeds to construct the virtual machine using the newly uploaded VHDs.

# Retrieve with Get-AzureSubscription 
$subscriptionName = '[MY SUBSCRIPTION]'  

# Retreive with Get-AzureStorageAccount 
$storageAccountName = '[MY STORAGE ACCOUNT]'   

# Specify the storage account location to store the newly created VHDs 
Set-AzureSubscription -SubscriptionName $subscriptionName -CurrentStorageAccount $storageAccountName 
 
# Select the correct subscription (allows multiple subscription support) 
Select-AzureSubscription -SubscriptionName $subscriptionName 

# Retreive with Get-AzureLocation 
$location = 'West US' 

# ExtraSmall, Small, Medium, Large, ExtraLarge
$instanceSize = 'Medium' 

# Has to be a unique name. Verify with Test-AzureService
$serviceName = '[UNIQUE SERVICE NAME]' 

# Server Name
$vmname1 = '[MY VM NAME]'

# Source VHDs
$sourceosvhd = 'C:\MyVHDs\AppServer1OSDisk.vhd'
$sourcedatavhd = 'C:\MyVHDs\AppServer1DataDisk.vhd'

# Target Upload Location 
$destosvhd = 'http://' + $storageAccountName + '.blob.core.windows.net/uploads/AppServer1OSDisk.vhd'
$destdatavhd = 'http://' + $storageAccountName + '.blob.core.windows.net/uploads/AppServer1DataDisk.vhd'

Add-AzureVhd -LocalFilePath $sourceosvhd -Destination $destosvhd 
Add-AzureVhd -LocalFilePath $sourcedatavhd -Destination $destdatavhd

Add-AzureDisk -OS Windows -MediaLocation $destosvhd -DiskName 'AppServer1OSDisk'
Add-AzureDisk -MediaLocation $destdatavhd -DiskName 'AppServer1DataDisk'

$migratedVM = New-AzureVMConfig -Name $vmname1 -DiskName 'AppServer1OSDisk' -InstanceSize 'Medium' |
					Add-AzureDataDisk -Import -DiskName 'AppServer1DataDisk' -LUN 0 |
					Add-AzureEndpoint -Name 'Remote Desktop' -LocalPort 3389 -Protocol tcp 
					
New-AzureVM -ServiceName $serviceName -Location $location -VMs $migratedVM 					

5 thoughts on “Migrate a Virtual Machine to Windows Azure with PowerShell

  1. Thanks. Is the code elided for clarity?
    For example Set-AzureSubscription requires -Certificate ( and shown in the script)

    • Additionally, in continuation to my previous question.

      Add-AzureDisk returns the following error despite calling Set and Select AzureSubscription prior to executing Add-AzureDisk.

      Add-AzureDisk : You MUST specify a subscription Id. Call Set-AzureSubscription and Select-AzureSubscription first.
      At line:1 char:1

    • Hi,

      Sorry, I forgot to mention to setup the PS cmdlets you will need to configure your subscription one of the following ways first.

      Get-AzurePublishSettingsFile # download publish settings
      Import-AzureSubscription … # import publish settings

      OR

      $subid = ‘yoursubid’
      $cert = Get-Item cert:\CurrentUser\My\CERTTHUMBPRINT
      Set-AzureSubscription -SubscriptionName $subname -Certificate $cert -SubscriptionID $subid

  2. I just did an Add-AzureVhd. The upload seems to have completed (the return on the screen had “Upload failed with exceptions: ” of nothing.

    I then tried Add-AzureDisk to register the OS disk and received the error: “The blob is not a valid VHD”

    Is there any way to determine what is ‘not right’.?
    Is there an assumption that is must be a fixed VHD that is uploaded? (I began with a dynamic vhd)

    The process seems very similar to the way csupload works, so I was a bit surprised to encounter the error on registration. As I have used csupload with Add-AzureDisk in the past without problem.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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