简体   繁体   中英

Remove/Delete all resources created while spinning a new VM through powershell

I have created a VM through powershell using template.json file. These are the list of resource types which gets created.
1. Virtual Machine
2. Network Interface
3. Network Security group
4. Disk

Now,what I do is update some apps on the newly created VM, generalize the VM and create an Image of the new VM. Till here I'm able to do it through powershell.

Now my requirement is to delete all the resources created during spinning the VM. I know the cmdlet to remove the VM is this.

Remove-VM -Name "new 2" -Force

Is there any single command to do this task?

Any help or input is highly appreciated. Thanks

Resource associated with the VM do not get deleted automatically when you delete the VM, primarily this is so you can re-use them if required, for example if you are deleting a VM but want to re-create it with the same VHD etc. This can be annoying if you know you want to delete the VM and associated resources. Other than manually deleting everything you really only have 2 options

  1. Delete the resource group - it helps here to scope your resource groups as small as possible to help make this possible

  2. Script something to delete the VM and resources for you, a good example is here .

I would suggest you to up vote an idea submitted by another Azure customer.

https://feedback.azure.com/forums/216843-virtual-machines/suggestions/8945692-delete-vm-with-all-associated-resources

All of the feedback that you share in these forum will be monitored and reviewed by the Azure team.

Use this excellent article. This is recommended article i have seen it in many technet forum replies.

Remove VM and its associated Resources

If you associate the VM and all related resources to a single resource group, simply removing that resource group will remove all attached resources and the VM it self.

You would simply do it like tat:

Get-AzResourceGroup -Name "ContosoRG01" | Remove-AzResourceGroup -Force

Here also, a complete blog post describing how to script the VM creation with AzureRM and powershell.

Create a Windows Server virtual machine with PowerShell

I have one script ready in below Link. Just input the VM name and script can handle the rest http://cloudcompute.info/delete-azure-vm-and-all-associated-resources-using-powershell-script/

Here is the complete script

Write-Host -NoNewline -ForegroundColor Green "Please enter the VM name you would like to remove:"
$VMName = Read-Host
$vm = Get-AzVm -Name $VMName
$RGName=$vm.ResourceGroupName
Write-Host -ForegroundColor Cyan 'Resource Group Name is identified as-' $RGName
$diagSa = [regex]::match($vm.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
Write-Host -ForegroundColor Cyan 'Marking Disks for deletion...'
$tags = @{"VMName"=$VMName; "Delete Ready"="Yes"}
$osDiskName = $vm.StorageProfile.OSDisk.Name
$datadisks = $vm.StorageProfile.DataDisks
$ResourceID= (Get-Azdisk -Name $osDiskName).id
New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
if ($vm.StorageProfile.DataDisks.Count -gt 0) {
    foreach ($datadisks in $vm.StorageProfile.DataDisks){
    $datadiskname=$datadisks.name 
    $ResourceID= (Get-Azdisk -Name $datadiskname).id 
    New-AzTag -ResourceId $ResourceID -Tag $tags | Out-Null
    }
}
if ($vm.Name.Length -gt 9) {
    $i = 9
} else {
    $i = $vm.Name.Length - 1
}
$azResourceParams = @{
    'ResourceName' = $VMName
    'ResourceType' = 'Microsoft.Compute/virtualMachines'
    'ResourceGroupName' = $RGName
}
$vmResource = Get-AzResource @azResourceParams
$vmId = $vmResource.Properties.VmId
$diagContainerName = ('bootdiagnostics-{0}-{1}' -f $vm.Name.ToLower().Substring(0, $i), $vmId)
$diagSaRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $diagSa }).ResourceGroupName
$saParams = @{
    'ResourceGroupName' = $diagSaRg
    'Name' = $diagSa
}
Write-Host -ForegroundColor Cyan 'Removing Boot Diagnostic disk..'
Get-AzStorageAccount @saParams | Get-AzStorageContainer | where {$_.Name-eq $diagContainerName} | Remove-AzStorageContainer -Force
Write-Host -ForegroundColor Cyan 'Removing Virtual Machine-' $VMName 'in Resource Group-' $RGName '...'
$null = $vm | Remove-AzVM -Force
Write-Host -ForegroundColor Cyan 'Removing Network Interface Cards, Public IP Address(s) used by the VM...'
foreach($nicUri in $vm.NetworkProfile.NetworkInterfaces.Id) {
    $nic = Get-AzNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $nicUri.Split('/')[-1]
    Remove-AzNetworkInterface -Name $nic.Name -ResourceGroupName $vm.ResourceGroupName -Force
foreach($ipConfig in $nic.IpConfigurations) {
        if($ipConfig.PublicIpAddress -ne $null) {
            Remove-AzPublicIpAddress -ResourceGroupName $vm.ResourceGroupName -Name $ipConfig.PublicIpAddress.Id.Split('/')[-1] -Force
        }
    }
}
Write-Host -ForegroundColor Cyan 'Removing OS disk and Data Disk(s) used by the VM..'
Get-AzResource -tag $tags | where{$_.resourcegroupname -eq $RGName}| Remove-AzResource -force | Out-Null
Write-Host -ForegroundColor Green 'Azure Virtual Machine-' $VMName 'and all the resources associated with the VM were removed sucesfully...'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM