Hassan Baig
Hassan Baig

Reputation: 15804

Retroactively joining existing VMs to a VNet (Azure)

I have two classic VMs provisioned in Azure. They exist in different resource groups, and I now I want to join them to a VNet. How do I do that? I can't exactly find documentation related to this, and since my application is live, I want to minimize downtime (or entirely eradicate downtime preferably).

Upvotes: 0

Views: 94

Answers (2)

Aatif Akhter
Aatif Akhter

Reputation: 2206

Your VM are in different Resource group, I will rather say your VM are in different cloud service as you VM are classic VM. You are not telling whether they are in same network or not, So I am assuming they are in different vNets. In such case NO need to migrate VM to a different cloud service. You just need to change one of the VM's vNet to the same as the different one.

You can easily do that using powershell script-

$vmName  = "xxxxx"
$srcServiceName  = "xxxxx"
$newVNet = "xxxxx"

# export vm config file
$workingDir = (Get-Location).Path
$sourceVm = Get-AzureVM –ServiceName $srcServiceName –Name $vmName
$global:vmConfigurationPath = $workingDir + "\exportedVM.xml"
$sourceVm | Export-AzureVM -Path $vmConfigurationPath

# remove vm keeping the vhds and spin new vm using old configuration file but in a  new vNet
Remove-azurevm –ServiceName $srcServiceName –Name $vmName
$vmConfig = Import-AzureVM -Path $vmConfigurationPath 
New-AzureVM -ServiceName $srcServiceName  -VMs $vmConfig -VNetName $newVNet -WaitForBoot 

You can also use a different approach in case your VM is an ARM VM. Refer to this question- http://stackoverflow.com/questions/39388871/move-a-microsoft-azure-vm-to-a-different-subnet-within-a-vnet/39419368#39419368

Upvotes: 1

Priyanka Makhija
Priyanka Makhija

Reputation: 168

There is no way to move an existing VM to a VNET. Though, there is something that you can do.

You can remove/delete the existing VM/VMs, doing so, you will be left with the VHD/VHDs of your VM/VMs. You can now go ahead and create new VM/VMs using the same VHD.

Having said that, there is no way to move an already created VM to a VNET. A VM can be joined to a VNET at the time of its creation.

Hope this helps!

Regards, Priyanka

Upvotes: 0

Related Questions