New Delhi , Moti Nagar
+91-9007641046
subhendu@subhendumct.com

Remove VM and Associated Resource Powershell

Live in Future - Live in Cloud

Remove VM and Associated Resource Powershell

Greetings to all unbelievable students across globe . Allow me to present one of the the most useful script for production use . We need to delete VMs and all associated resources like disks+NIC for various reason . But most of the time we just delete the VM and forget to delete VHDs or NICs . That create problem in future and disk cost money  . This script is a ultimate solution for VM deletion . Beauty of this script is you dont have to mention VM name at the beginning . Script will prompt you for the input . Type the VM name , sit back and relax . Script will collect all required information from the VM name and will pass in the script when needed . You can run this script directly in production . It will not interraapt anything unless there is any dependency with VM .  

Only limitation of this script is this script will work with only one VM at a single time . If you want to delete multiple VM at a single time please mail  me. 

Few point to keep in mind :

  • Please run Powershell ISE in admin mode and copy and paste this script in scripting environment .
  • Make sure you have installed and imported AzureRM module.
  • You should have access to fetch VM details in production . I am owner of my EA subscription .
clear
Write-Host "Hey $env:UserName Greetings !!" -ForegroundColor Cyan 
Start-Sleep -s 1
Write-Host "Welcome @ Subhendu's Automation System ." -ForegroundColor Cyan 
Start-Sleep -s 1
Write-Host "Remember this script will remove VM,VM OS Disk,Data Disks and NIC card ." -ForegroundColor Cyan
Start-Sleep -s 1
Write-Host "Can you please tell me the VM Name ?" -ForegroundColor Cyan
Start-Sleep -s 1  
Add-Type -AssemblyName Microsoft.VisualBasic
$VMlist = [Microsoft.VisualBasic.Interaction]::InputBox('Please type VM name', 'Build By Subhendu V1', "Please Type VM name")
$king = '---------------------------------------------------------------------------------------'
Write-Host "Great !! So I will continue with "$VMlist" vm" -ForegroundColor Yellow
Start-Sleep -s 1
Write-Host "Now you can sit back with a cup of coffee and relax . Process will notify you once the entire work is done." -ForegroundColor Cyan
#$VMlist = "VM1","VM2"- mention all VM name that you want to remove .
$i = 1
ForEach($VMlist in $VMlist)
{
$VMName = $VMlist
$VMRGname = (Get-AzureRmResource -Name $VMName -ResourceType "Microsoft.Compute/virtualMachines").ResourceGroupName
$OSDisk = (Get-AzureRMVM –Name $VMName –ResourceGroupName $VMRGname).StorageProfile.OsDisk.Name
$DataDisk = (Get-AzureRMVM –Name $VMName –ResourceGroupName $VMRGname).StorageProfile.DataDisks.name
$VMNICdetails = (Get-AzureRmVM -ResourceGroupName $VMRGname -Name $VMName).NetworkProfile.NetworkInterfaces.id 
$VMNic = (Get-AzureRmResource -ResourceId $VMNICdetails).Name
Write-Host "VM No $i and VM name is $VMName"
Remove-AzureRmVM -ResourceGroupName $VMRGname -Name $VMName -Force
Write-Host "NIC is $VMNic"
Remove-AzureRmNetworkInterface -Name $VMNic -ResourceGroupName $VMRGname -Force
ForEach($OSDisk in $OSDisk)
{
Write-Host "OS-Disk No $i : $OSDisk"
Remove-AzureRmDisk -ResourceGroupName $VMRGname -DiskName $OSdisk -Force
}
ForEach($DataDisk in $DataDisk)
{
Write-Host "Data-Disk No $i : $DataDisk"
Remove-AzureRmDisk -ResourceGroupName $VMRGname -DiskName $DataDisk -Force
$i = $i+1
}
$i = $i+1
}

Leave a Reply

Your email address will not be published. Required fields are marked *