Hi,

I often hear from my customers and the community “Hey, why I can’t change the VNet from an Azure VM easily”. That statement isn’t really true, and I want to clarify why.

First of all, normally there is no reason to change the network from an Azure VM. To change the subnet is more plausible than a VNet change. Why?

When you design your network infrastructure you often use a hub and spoke architecture which means, you should use Azure VNet peering to connect each virtual network and have one point where you connect to your On-Prem environment. Here is an overview of a hub and spoke architecture.

Hub-spoke topology in Azure
Source: https://docs.microsoft.com/

So why you should change the VNet of an existing VM, because each VM can connect? But in some situations, you’ve deployed a new VM, and maybe (if it’s not deployed over IaC) you’ve chosen the wrong Azure VNet. In that case, I’ll demonstrate the possibilities that you have in the next sections.

Using the Azure portal

When you’ve created an Azure VM with the wrong VNet settings, you have the ability to change the VNet over the Azure portal.

VM DOWNTIME!!

Keep in mind, when you use that option, the Azure VM is down for a few minutes

Open the Azure portal, select the VM with the wrong configurations, and select delete.

No worries, the attached disk won’t be deleted!

Now you have to remove the “old” network interface too. Please select the network interface and then delete it.

Okay, there is only the Azure managed disc available from the Azure VM. Please select the disk and choose the option “+ Create VM”.

Fill out the wizard settings with the new name and the right Virtual network settings.

That’s it, it’s really easy!

Using Automation

VM DOWNTIME!!

Keep in mind, when you use that option, the Azure VM is down for a few minutes

In some situations, you want to fix that issue with an automation script. No problem, I’ve created a small PowerShell script to fulfill that task. You can find that script on my GitHub repo.

It’s really simple, when you start the script, first of all, you have to login into your Azure account

Then, you have to select the right subscription

Now, select the VM to change

IMPORTANT

If your VM have more then one network interface attached, the script exit. At the moment I only support VMs with one network interface!

Now, select the destination Azure VNet followed by the subnet

In the end, you get an overview and have to approve the changes

The script completes the following tasks:

  • Stop the VM
  • Remove VM
  • Remove assigned network card
  • Create new VM including
    • New network card
    • Existing managed disc
    • Into the new VNet

That’s it, also really simple and it takes a few minutes!