共用方式為


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
VHD URI: https://ARMSTORAGE.blob.core.windows.net/vhds/linuxvm.vhd

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:
Add-AzureRmAccount

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>"
$location="<Location>"

Create a new VM config:

$vmName="<VM Name>"
$vmSize="<VM Size>"
$vmc = New-AzureRmVmConfig -VMName $vmName -VMSize $vmSize

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-AzureRmVmNetworkInterface -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:

$rgName="ARMGROUP"
$location="southcentralus"
$vmName="LinuxVM"
$vmSize="Standard_A1"
$vmc = New-AzureRmVmConfig -VMName $vmName -VMSize $vmSize
$nic = Get-AzureRmNetworkInterface -Name "linuxvm-nic" -ResourceGroupName $rgName
Add-AzureRmVmNetworkInterface -VM $vmc -Id $nic.Id
$vhdName="linuxvm.vhd"
$vhdURI="https://linuxstoragelrs.blob.core.windows.net/vhds/linuxvm.vhd"
Set-AzureRmVmOSDisk -VM $vmc -Name $vhdName -VhdUri $vhdURI -Linux -CreateOption Attach
New-AzureRmVm -ResourceGroupName $rgName -Location $location -VM $vmc

Comments

  • Anonymous
    May 19, 2017
    Too many typos. Your code is not consistent in the use of variable names. Please check it.
    • Anonymous
      May 19, 2017
      Hi Ahmed,Could you let me know where you are having issues?We use this all the time and it's been very consistent.Thank you!-Marco