How to move a virtual machine to a new virtual network using PowerShell
Overview
When creating a virtual machine you must assign the VM to a virtual network. Once assigned, Microsoft does not support VNet-to-VNet migration, so the VM must be redeployed to the new virtual network.
The following article shows you how to move a virtual machine to a new virtual network on UKCloud for Microsoft Azure.
Warning
Running the script below will result in downtime for your virtual machine as it needs to be removed then recreated.
Prerequisites
Before you begin, ensure your PowerShell environment is set up as detailed in Configure the Azure Stack Hub user's PowerShell environment.
Moving a virtual machine to a new virtual network
Declare variables
Enter details below to provide values for the variables in the following script in this article:
Variable name | Variable description | Input |
---|---|---|
$ArmEndpoint | The Azure Resource Manager endpoint for Azure Stack Hub | |
$RGName | Name of the resource group that the VM exists in | |
$VMName | Name of the virtual machine to move to a new virtual network | |
$NewNetworkName | Name of the virtual network to move the virtual machine to | |
$NewSubnetName | Name of the subnet to move the virtual machine to | |
$NewPrivateIp | New private IP for the virtual machine | |
$NewNicName | Name of the new network interface card to create | |
$NewPublicIpName | Name of the new public IP address to create |
Move the virtual machine
From your PowerShell window:
# Initialise environment and variables
# Declare endpoint
$ArmEndpoint = ""
## Add environment
Add-AzEnvironment -Name "AzureStackUser" -ArmEndpoint $ArmEndpoint
## Login
Connect-AzAccount -EnvironmentName "AzureStackUser"
# Get location of Azure Stack Hub
$Location = (Get-AzLocation).Location
# Input Variables
$RGName = ""
$VMName = ""
$NewNetworkName = ""
$NewSubnetName = ""
$NewPrivateIp = ""
$NewNicName = ""
$NewPublicIpName = ""
# Retrieve VM details
$VM = Get-AzVM -Name $VMName -ResourceGroupName $RGName
# Check if the VM uses managed or unmanaged disks
if ($VM.StorageProfile.OsDisk.ManagedDisk) {
$ManagedDisks = $true
}
else {
$ManagedDisks = $false
}
# Stop the VM
Stop-AzVM -Name $VMName -ResourceGroupName $RGName -Force
# Get VM current networking details
$Nic = (Get-AzNetworkInterface | Where-Object -FilterScript { $_.VirtualMachine.Id -like $VM.Id })
$PublicIp = Get-AzPublicIpAddress | Where-Object -FilterScript { $_.Id -like $Nic.IpConfigurations.PublicIpAddress.Id }
# Get new virtual network details
$NewVNet = Get-AzVirtualNetwork | Where-Object -FilterScript { $_.Name -like $NewNetworkName }
$NewSubnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $NewVNet -Name $NewSubnetName
# Create new VM networking resources
$NewPublicIp = New-AzPublicIpAddress -Name $NewPublicIpName -Location $NewVNet.Location -ResourceGroupName $NewVNet.ResourceGroupName -Sku $PublicIp.Sku.Name -AllocationMethod $PublicIp.PublicIpAllocationMethod
$NewIpConfig = New-AzNetworkInterfaceIpConfig -Subnet $NewSubnet -Name "ipconfig2" -Primary -PrivateIpAddress $NewPrivateIp -PublicIpAddress $NewPublicIp
$NewNic = New-AzNetworkInterface -Name $NewNicName -ResourceGroupName $NewVNet.ResourceGroupName -Location $NewVNet.Location -IpConfiguration $NewIpConfig -NetworkSecurityGroupId $Nic.NetworkSecurityGroup.Id -Force
# Retrieve VM data disk details
if ($VM.StorageProfile.DataDisks) {
$Lun = 0
if ($ManagedDisks) {
$Disks = Get-AzDisk -ResourceGroupName $RGName | Where-Object -FilterScript { $_.ManagedBy -like "*$VMName" } | Where-Object -FilterScript { $_.Id -notlike $VM.StorageProfile.OsDisk.ManagedDisk.Id }
}
else {
$Disks = $VM.StorageProfile.DataDisks
}
}
# Create new VM configuration
$NewVmConfig = New-AzVMConfig -VMName $VMName -VMSize $VM.HardwareProfile.VmSize
if ($ManagedDisks) {
# Add OS disk to new VM configuration
if ($VM.StorageProfile.OsDisk.OsType -eq "Windows") {
$NewVmConfig = Set-AzVMOSDisk -VM $NewVmConfig -ManagedDiskId $VM.StorageProfile.OsDisk.ManagedDisk.Id -CreateOption "Attach" -Windows
}
else {
$NewVmConfig = Set-AzVMOSDisk -VM $NewVmConfig -ManagedDiskId $VM.StorageProfile.OsDisk.ManagedDisk.Id -CreateOption "Attach" -Linux
}
# Add data disk(s) to new VM configuration
foreach ($Disk in $Disks) {
$NewVmConfig = Add-AzVMDataDisk -VM $NewVmConfig -ManagedDiskId $Disk.Id -CreateOption "Attach" -Lun $Lun -DiskSizeInGB $Disk.DiskSizeGB
$Lun++
}
}
else {
# Add OS disk to new VM configuration
if ($VM.StorageProfile.OsDisk.OsType -eq "Windows") {
$NewVmConfig = Set-AzVMOSDisk -VM $NewVmConfig -VhdUri $VM.StorageProfile.OsDisk.Vhd.Uri -CreateOption Attach -Name $VM.StorageProfile.OsDisk.Name -Windows
}
else {
$NewVmConfig = Set-AzVMOSDisk -VM $NewVmConfig -VhdUri $VM.StorageProfile.OsDisk.Vhd.Uri -CreateOption Attach -Name $VM.StorageProfile.OsDisk.Name -Linux
}
# Add data disk(s) to new VM configuration
foreach ($Disk in $Disks) {
$NewVmConfig = Add-AzVMDataDisk -VM $NewVmConfig -Name $Disk.Name -CreateOption "Attach" -Lun $Lun -DiskSizeInGB $Disk.DiskSizeGB
$Lun++
}
}
# Add new NIC to new VM configuration
$NewVmConfig = Add-AzVMNetworkInterface -VM $NewVmConfig -NetworkInterface $NewNic
# Remove the old VM
Write-Output -InputObject "Removing old virtual machine"
Remove-AzVM -Name $VMName -ResourceGroupName $RGName -Force
# Create the new VM from the new VM configuration
Write-Output -InputObject "Creating new virtual machine"
New-AzVM -VM $NewVmConfig -ResourceGroupName $NewVNet.ResourceGroupName -Location $NewVNet.Location
Get-AzVM -ResourceGroupName $RGName -Name $VMName
Write-Output -InputObject "The virtual machine has been created successfully"
Feedback
If you find a problem with this article, click Improve this Doc to make the change yourself or raise an issue in GitHub. If you have an idea for how we could improve any of our services, send an email to feedback@ukcloud.com.