azurevmdelete.ps1
<#PSScriptInfo .VERSION 1.0 .GUID 456b68c8-b794-49b4-b5c6-54f5ef22e941 .AUTHOR Manu Philip .EMAIL manu@exchangeonline.in .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Procedure 1. Copy the full script in to a notepad and save as .ps1 file 2. Open PowerShell as Administrator 3. Connect your Azure tenant 4. Run the script in PowerShell 5. Script will prompt to enter the VM Name. Enter the VM name and sit relax until the script completes the full deletion of all your resources Notes: The script will not delete the network security group, Storage Account and Virtual Network as there can be other VMs attached to these resources The Script is made with extra precautions and tested well, to avoid any unexpected deletions. However, Appreciated to test the script in your test infrastructure before using in Production critical systems #> Param() 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...' |