Move an Azure IaaS VM between vNets

Recently i had a requirement to move a number of VM’s from one Azure vNet to another. This isnt possible in the current Azure Portal (although hopefully is coming based on the comments on the Azure Advisory forums – https://feedback.azure.com/forums/34192–general-feedback), therefore you will need to use PowerShell to do this.

Warning: This action does incur downtime, the method will remove the VM, keeping the disk and create a new VM attaching the kept disk. The disk should keep any installed applications, features or roles.

<# 

Author: HP, July 2017.

 
 

Move a VM that has un-managed disks between vNets: 

 
 

1. Delete VM from the portal. 

2. Create a NIC in the new vNet. 

3. Get the NIC name and add it to variable: $NIC1. 

4. Ensure all other variables are filled out 

5. Ensure that the correct subscription is your default with (Get-AzureRmContext).Subscription.  

   If wrong set with Set-AzureRmContextSubscriptionName “SUBID” –TenantId “TENANTID” 

6. Run below script. 

 
 

#> 

 
 

# To login to Azure Resource Manager 

Login-AzureRmAccount 

          

# To view all subscriptions for your account 

#Get-AzureRmSubscription 

          

# To select a default subscription for your current session 

#Get-AzureRmSubscriptionSubscriptionID “SUBID” | Select-AzureRmSubscription 

          

$RGName = “Resource_Group_Name 

$VMLocation = “VM_Location 

$VMSize = “Enter_VM_Size 

$VMName = “VM_Name 

$VM = New-AzureRmVMConfigVMName $VMNameVMSize $VMSize; 

           

$NIC1 = Get-AzureRmNetworkInterface -Name (“VM_Name“) –ResourceGroupName $RGName; 

$NIC1Id = $NIC1.Id; 

#Uncomment if deploying with Multiple NICs 

#$NIC2 = Get-AzureRmNetworkInterface -Name (“NIC2Name”) –ResourceGroupName $RGName; 

#$NIC2Id = $NIC2.Id; 

           

$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NIC1Id; 

#Uncomment if deploying with Multiple NICs 

#$VM = Add-AzureRmVMNetworkInterface -VM $VM -Id $NIC2Id;              

           

$osDiskName = “Disk_Name 

$osDiskVhdUri = “Enter_VHD_URL.vhd 

           

$VM = Set-AzureRmVMOSDisk -VM $VM –VhdUri $osDiskVhdUri -name $osDiskNameCreateOption attach -Windows 

           

New-AzureRmVMResourceGroupName $RGName -Location $VMLocation -VM $VM -Verbose 

In total this will take around 20 minutes a VM. There are ofcourse some things to be mindful of, and i will list them below:

  • You will need to reconfigure your VM Extensions to any VM’s you move.
  • NIC’s in the new vNet will need to be re-created.
  • You will need to have necessary permissions to the relevant subscriptions to move resources.
  • Always test a move pre doing this in your live environment.

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 )

Google+ photo

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

Connecting to %s