How to transfer VMs on Microsoft Azure
Let’s assume that we have two resource groups or two subscriptions on Microsoft Azure and there are virtual machines (VMs) and other components that necessary for them to operate correctly, like network interface cards, network security groups, and static public IP address, etc.
In this article, we will clone the virtual machine from one resource group to another one. We will finally get together all scripts written on Powershell.
First of all, I have logged on to my Azure account on Powershell.
Then I have created two resource groups called rgA and rgB.
After that, I have created Linux VM using Azure Console. You can see the details of the VM below. nsgA has been set as ports 22 and 80 open, and pipA has been created as a static public IP address.
In this step, we have totally of seven resources on rgA.
Now we should create a snapshot from VM using PowerShell command.
Then, I have created a disk for a new VM on rgB. Here all PowerShell codes that I used.
After that, I have transferred all other VM components from rgA to rgB and assigned them to a new VM on rgB using Powershell.
Finally, we have successfully transferred our VM from one resource group to another.
You can find all related PowerShell codes below.
#Set the subscription for the current session where the commands wil execute
Select-AzureRmSubscription -SubscriptionId '4407ecf0-6e70-4245-a8f7-e12ee689811e'# Get existing VM from which to clone from
$sourceVirtualMachine = Get-AzVM -ResourceGroupName rgA -Name vmA# Create a new VM Disk Snapshot
$snapshot = New-AzSnapshotConfig -SourceUri $sourceVirtualMachine.StorageProfile.OsDisk.ManagedDisk.Id -Location "EastUS" -CreateOption copy
$snapshot = New-AzSnapshot -Snapshot $snapshot -SnapshotName vmASnapshot -ResourceGroupName rgA# Create a new managed disk from the snapshot
$disk = New-AzureRmDiskConfig -AccountType StandardSSD_LRS -DiskSizeGB 30 -Location "EastUS" -CreateOption Copy -SourceResourceId $snapshot.Id
$disk = New-AzureRmDisk -Disk $disk -ResourceGroupName rgB -DiskName vmACloned# List the related resourcesId
$nicA = Get-AzNetworkInterface -ResourceGroupName "rga" -Name "vma371"
$nsgA = Get-AzNetworkSecurityGroup -Name "vmA-nsg" -ResourceGroupName "rga"
$pipA = Get-AzureRmPublicIpAddress -Name "pipA" -ResourceGroupname "rga"
$vnetA = Get-AzVirtualNetwork -Name "vnetA" -ResourceGroupName "rga"# move all vmA related resources from rgA to rgB
Move-AzResource -DestinationResourceGroupName "rgb" `
-ResourceId $nicA.Id, $nsgA.Id, $pipA.Id, $vnetA.Id# Initialize virtual machine configuration
$targetVirtualMachine = New-AzureRmVMConfig -VMName vmB -VMSize Standard_B1ls#Attach Managed Disk to target virtual machine. OS type depends OS present in the disk (Linux)
$diskB = Get-AzureRmDisk -ResourceGroupName "rgB"
$targetVirtualMachine = Set-AzureRmVMOSDisk -VM $targetVirtualMachine -ManagedDiskId $diskB.Id -CreateOption Attach -Linux# Get and Set network interface card
$nic = Get-AzNetworkInterface -ResourceGroupName "rgB" -Name "vma371"
$targetVirtualMachine = Add-AzVMNetworkInterface -VM $targetVirtualMachine -Id $nic.Id#get netwrok security group and set vmB
$nsg = Get-AzNetworkSecurityGroup -Name vmA-nsg -ResourceGroupName "rgB"
$targetVirtualMachine = Set-AzNetworkSecurityGroup -NetworkSecurityGroup $nsg#Get Virtual Network information and set vmB
$vnet = Get-AzVirtualNetwork -Name vnetA -ResourceGroupName "rgB"
$targetVirtualMachine = Set-AzVirtualNetwork -VirtualNetwork $vnet# Create a new VM from cloned vmA snapshot
New-AzureRmVM -ResourceGroupName "rgB" -Location "EastUS" -VM $targetVirtualMachine -Verbose