Step-By-Step: Move a Microsoft Azure VM to a Different Subnet Within a vNet

Step-By-Step: Move a Microsoft Azure VM to a Different Subnet Within a vNet

  • Comments 3
  • Likes

Hello Folks,2

On May 2nd we help a Microsoft Azure Camp in the MPR (Multi-Purpose Rooms) of our head office in Mississauga Ontario.  We had 112 enthusiastic attendees and we covered the following topics:

  1. Introduction to Windows Azure
  2. Cloud Storage and Virtual Networks
  3. Virtual Machines
  4. Dealing with DEV/TEST scenarios
  5. Monitoring Public and Hybrid Clouds
  6. Windows Azure Active Directory

the session was recorded and will be available soon on this blog.  but for now,  let jump into one question that was asked during the Virtual Networks section.

One attendee asked:

“can you move a machine from one vNet to another without having to re-install it?”

Yes you can...  The process to carry out a move of your VM to a different subnet is straight forward:

  1. Migrate a VM from one subnet to another.
  2. Update the VM configuration and restart the VM.

Let just step through it.

1- Create a virtual Lets start with a VM created on Subnet-1 of our Public Virtual Network

We already covered in Step-By-Step: Create a Site-to-Site VPN between your network and Azure how to create a Virtual Network with multiple subnets.

2- Create a Virtual Machine in one of the subnets you just created.

In this step we’ll use the “Create from Gallery” option in Azure. 

In the third page of the wizard ensure that under REGION/AFFINITY GROUP/VIRTUAL NETWORK section you select the virtual network you just created and select one of the defined subnet.

image

Complete the machine creation and wait for it to be provisioned and running.  Once this machine is created use a PowerShell Script to move it where you need it

3- Use PowerShell to move the machine to a different subnet.

You need to use the following script to move the machine.  This particular script imports the Azure Module, adds your Azure Account to the PowerShell session, it asks for the info needed:

  • Cloud Service name
  • The name of the VM to move
  • And the destination subnet to move it to.

# Import Windows Azure PowerShell Module
Import-Module Azure

# Login to Windows Azure subscription
Add-AzureAccount

# collect info for the VM move
$ServiceName= read-host -Prompt ("Enter your Cloud Service name")
$VM= read-host -Prompt ("Enter your VM name")
$Subnet= read-host -Prompt ("Enter your destination Subnet name")

# Move the VM
Get-AzureVM -ServiceName $ServiceName -name $VM | Set-AzureSubnet -SubnetNames $Subnet | Update-AzureVM

Prior to moving the VM I validated it’s internal IP address and subnet.

image

After running the script in this example you can see the Succeeded Operation Status.

image

Once we received the Succeeded results I refreshed my Azure Portal session and validated the internal IP address and Subnet.

image

That’s it.  Again a great example of how PowerShell brings the power to manage Microsoft Azure in a very straight forward manner.

To continue your learning with Microsoft Azure, visit Microsoft Virtual Academy and complete the Azure for IT Pros Jump Start.

Cheers!

clip_image011

Pierre Roman | Technology Evangelist
Twitter | Facebook | LinkedIn

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • So that's how it's done. Very handy to know. Thanks for sharing guys.

  • I'm wondering if the question wasn't referring to vNet as virtual network and not subnet...
    What is the official process for migrating a VM from one virtual network to another (not subnets within the same virtual network)?

  • Hey Gustavo,

    I'm looking into that as we speak. This will be a post in the upcoming weeks

    P.