Rename a VM or Create a new VM with existing os-disk
There is no option to rename the VM in azure. As a work-around, you will have to delete and re-create the VM.
Either we will re-create machine from backup vault or from disk of existing VM. Like rie-dev-01-rest in this case.
copy os-disk to a container. If storage account is same for a machine which OS disk is to be copied, then no need to turn off this machine. If storage accounts are different then we need to stop machine to copy os-disk.
Refer to video “Copying OS disk to create a new vm” for copying os-disk
We need to run these commands 1 by 1 preferably.
$rgname = "ReynoldsIE" # resource group name
$locname = "North Europe" # resource location
$saname = "reynoldsie" # storage acount name
$vnetname = "AzureLAN" #Existing VNET name
$subnetName ="ReynoldsIESubnet" # azure subnetname
$vnet= Get-AzureRmVirtualNetwork -Name $vnetname -ResourceGroupName $rgname
$singleSubnet = Set-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet -AddressPrefix 10.0.0.0/24 (IP range of same of existing machine)
$vmname = "r-dev01"
$vmsize = "Standard_B2ms" #Size of the VM
$vm=New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize
$storageacc= Get-AzureRmStorageAccount -ResourceGroupName $rgname -Name $saname
$diskname = $vmname + "-OSDisk" (This will name of disk)
$vhdURI = "https://reynoldsie.blob.core.windows.net/r-dev01rename/riedev01rest-osdisk-20190219-182907.vhd"
$nicname ="R-Dev01NIC" #New NIC name
$pip=New-AzureRmPublicIpAddress -Name $nicName -ResourceGroupName $rgName -DomainNameLabel $domName -Location $locName -AllocationMethod Static # if already existing mention the public IP name.
$subnetindex =0
$nic=New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $locName -SubnetId $vnet.Subnets[$subnetIndex].Id -PublicIpAddressId $pip.Id
$vm = New-AzureRMVMConfig -VMName $vmName -VMSize $vmSize
$vm = Add-AzureRMVMNetworkInterface -VM $vm -Id $nic.Id
$vm = Set-AzureRmVMOSDisk -VM $vm -Name $diskname -VhdUri $vhdURI -CreateOption attach -Windows
New-AzureRmVM -ResourceGroupName $rgname -Location $locname -VM $vm
It will create new machine with name mentioned above.