Share via


How to rebuild an Azure VM from an existing OS disk (ARM)

Use the below script to recreate a ARM VM via PowerShell. Modify the highlighted sections only

 

#Required Variables
$subID = 'SubscriptionID'
$rgname = 'ResourceGroupName'
$loc = 'Location'
$vmsize = 'VirtualMachineSize'
$vmname = 'VirtualMachineName'
$nic1 = 'NetworkInterface1Name'
$osDiskName = 'DiskOSName'
$osDiskVhdUri = 'DiskOSUri'

 

#Optional Variables to add additional Network Interfaces
#$nic2 = 'NetworkInterface2Name'
#$nic3 = 'NetworkInterface3Name'

 

# To login to Azure Resource Manager
Login-AzureRmAccount

# To view all subscriptions for your account
Get-AzureRmSubscription

# To select a default subscription for your current session
Get-AzureRmSubscription –SubscriptionID “$subID” | Select-AzureRmSubscription

 

$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize;
$nic1 = Get-AzureRmNetworkInterface -Name ($nic1) -ResourceGroupName $rgname;
$nic1Id = $nic1.Id;
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic1Id;

 

#Uncomment to add additonal Network Interfaces
#$nic2 = Get-AzureRmNetworkInterface -Name ($nic2) -ResourceGroupName $rgname;
#$nic2Id = $nic2.Id;
#$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic2Id;
#$nic3 = Get-AzureRmNetworkInterface -Name ($nic3) -ResourceGroupName $rgname;
#$nic3Id = $nic3.Id;
#$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic3Id

 

#If the VM in question is a Linux box, change from -Windows to -Linux
$vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption attach -Windows

 

New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm -Verbose