Share via


How to Rebuild from an Existing Managed Disk

If Azure PowerShell is not installed, please install it from https://azure.microsoft.com/en-us/downloads/ then modify and run the following script:

# 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 'SUBSCRIPTONID' | Select-AzureRmSubscription

#Fill in all variables
$subid = "SUBSCRIPTION ID"
$rgName = "RESOURCE GROUP NAME";
$loc = "LOCATION";
$vmSize = "SIZE";
$vmName = "DESIRED VM NAME;
$nicName = "NIC NAME";
#$avName = "AV SET NAME";
$osDiskName = "OS DISK NAME";
$osDiskResouceId = "OS DISK RESOURCE ID";
#$dataDiskName = "DATA DISK NAME";
#$dataDiskResourceId = "DATA DISK RESOURCE ID";

#Comment/uncomment based on feature needs

#No AV Set
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize;
#AV Set
#$avSet = Get-AzureRmAvailabilitySet –Name $avName –ResourceGroupName $rgName;
#$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize -AvailabilitySetId $avSet.Id;

#Get NIC Resource Id and add
$nic = Get-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName;
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id;

#Windows VM
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $osDiskResouceId -name $osDiskName -CreateOption Attach -Windows;

#Linux VM
#$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskId $osDiskResouceId -name $osDiskName -CreateOption Attach -Linux;

#Data Disks - optional
#Add-AzureRmVMDataDisk -VM $vm -ManagedDiskId $dataDiskResourceId -Name $dataDiskName -Caching None -DiskSizeInGB 1023 -Lun 0 -CreateOption Attach;

New-AzureRmVM -ResourceGroupName $rgName -Location $loc -VM $vm;