Azure Linux Support Team

Support for Linux on Azure

Azure PowerShell: How to delete and re-deploy a VM from VHD

Before moving further, please make sure you have Azure PowerShell installed:
How to install and configure Azure PowerShell

For basic information about using Azure Powershell with Resource Manager (ARM), please have a look at:
Using Azure PowerShell with Azure Resource Manager

In this example, the following variables are being used:

Resource Group: ARMGROUP
Original VM Name: LinuxVM
Location: southcentralus
NIC Name: LinuxVM-NIC
Storage Account: ARMSTORAGE

You should be able to get a list of parameters from a given VM by using:
Get-AzureRmVm -ResourceGroupName $rgName -Name $vmName

It also assumes that you have logged in with the azure powershell using:

And that  your subscription is set using:
Set-AzureRmContext -SubscriptionID <YourSubscriptionId>

Deleting a VM on ARM using PowerShell
Remove-AzureRmVm -Name “LinuxVM” -Force -ResourceGroupName “ARMGROUP”

Recreating the VM from the Original VHD:

Define Resource Group and Location:

$rgName=”<Resource Group>”

Create a new VM config:

$vmName=”<VM Name>”
$vmSize=”<VM Size>”
$vmc = New-AzureRmVmConfig -VMName $vm_name -VMSize $vm_size

Add a network interface (in this example, we are reusing the existing NIC from the previous VM)
$nic = Get-AzureRmNetworkInterface -Name “<NIC Name>” -ResourceGroupName $rgName
Add-AzureRmNetworkInterface -VM $vmc -Id $nic.Id

Set the OS disk:

$vhdName=”<VHD Name>”
$vhdURI=”<VHD URI>”
Set-AzureRmVmOSDisk -VM $vmc -Name $vhdName -VhdURI $vhd_uri -Linux -CreateOption Attach

Create the VM

New-AzureRmVm -ResourceGroupName $rgName -Location $location -VM $vmc

Example with the parameters in this article:

$vmc = New-AzureRmVmConfig -VMName $vmName -VMSize $vmSize
$nic = Get-AzureRmNetworkInterface -Name “linuxvm-nic” -ResourceGroupName $rgName
Add-AzureRmVmNetworkInterface -VM $vmc -Id $nic.Id
Set-AzureRmVmOSDisk -VM $vmc -Name $vhdName -VhdUri $vhdURI -Linux -CreateOption Attach
New-AzureRmVm -ResourceGroupName $rgName -Location $location -VM $vmc