In this case I wanted to move Azure VM from one cloud service to another cloud service in same Azure Subscription. Using Microsoft Azure Web Portal this task can be achieved in following three steps:
Set-AzureSubscription -SubscriptionName 'Name of the subscription' -CurrentStorageAccount 'teststorageaccount'
$vmName='test'
$disk0Name = 'test-os-disk0'
$disk1Name = 'test-data-disk-1'
$disk2Name = 'test-data-disk-2'
$vNetName = 'test-VirtualNetwork'
$subNet = 'Sub-1'
$cloudSvcName = 'test-Cloud-Service2'
$vm1 = New-AzureVMConfig -DiskName $disk0Name -InstanceSize Medium -Name $vmName -Label $vmName |
Add-AzureDataDisk -DiskName $disk1Name -Import -LUN 0 |
Add-AzureDataDisk -DiskName $disk2Name -Import -LUN 1 |
Set-AzureSubnet $subNet |
Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 3390 |
Add-AzureEndpoint -LocalPort 5986 -Name 'WinRmHTTPs' -Protocol tcp -PublicPort 5987 |
Add-AzureEndpoint -LocalPort 80 -Name 'http' -Protocol tcp -PublicPort 80 |
Add-AzureEndpoint -LocalPort 443 -Name 'https' -Protocol tcp -PublicPort 443
New-AzureVM -ServiceName $cloudSvcName -VMs $vm1 -VNetName $vNetName
In bold are the two additional data disk, that will be attached during creation of the VM in the new cloud service.
- Note the disk(s) that were attached on Azure VM, and other configuration settings like VM size, virtual network, endpoints and so on.
- Delete the Azure VM with option to keep the attached disks
- Create new VM from Gallery, and on first Wizard page (Chose an Image) select MY DISKS option, and select the disk noted in first step. Complete the wizard with assigning the VM to the new Cloud Service.
- Setting the currentStorageAccount for the Azure Subscription
- Setting basic variables for the VM, like Name, disks, virtual network, subnet and cloud service
- Creating new configuration for the VM
- And, finally creating the new VM
Set-AzureSubscription -SubscriptionName 'Name of the subscription' -CurrentStorageAccount 'teststorageaccount'
$vmName='test'
$disk0Name = 'test-os-disk0'
$disk1Name = 'test-data-disk-1'
$disk2Name = 'test-data-disk-2'
$vNetName = 'test-VirtualNetwork'
$subNet = 'Sub-1'
$cloudSvcName = 'test-Cloud-Service2'
$vm1 = New-AzureVMConfig -DiskName $disk0Name -InstanceSize Medium -Name $vmName -Label $vmName |
Add-AzureDataDisk -DiskName $disk1Name -Import -LUN 0 |
Add-AzureDataDisk -DiskName $disk2Name -Import -LUN 1 |
Set-AzureSubnet $subNet |
Add-AzureEndpoint -LocalPort 3389 -Name 'RDP' -Protocol tcp -PublicPort 3390 |
Add-AzureEndpoint -LocalPort 5986 -Name 'WinRmHTTPs' -Protocol tcp -PublicPort 5987 |
Add-AzureEndpoint -LocalPort 80 -Name 'http' -Protocol tcp -PublicPort 80 |
Add-AzureEndpoint -LocalPort 443 -Name 'https' -Protocol tcp -PublicPort 443
New-AzureVM -ServiceName $cloudSvcName -VMs $vm1 -VNetName $vNetName
In bold are the two additional data disk, that will be attached during creation of the VM in the new cloud service.