We all know the perils of working with the new Azure Portal. Although, it is a very well designed portal, it still lacks some basic functionality. Specifically, if we need to work with multiple resources at once.
I will address three of the most common shortcomings in the Azure Portal with this script:
- The ability to delete attached disks when deleting a virtual machine. This feature was available in the the old Azure management portal, but was somehow left out of the new portal (maybe intentionally to make the portal idiot proof!). The following script will give you an option to delete disks when deleting virtual machine(s).
- The ability to delete multiple virtual machines at once (this problem is common with performing any operation on multiple resources at once, but this script with address it for the delete operation). The following script will allow you to select a single or multiple virtual machines for deletion.
- This Script will also delete the Network Interface Cards attached to the virtual machine(s).
#Login to your Azure account
Add-AzureRmAccount
#Select Subscription
$subId = (Get-AzureSubscription | Out-GridView -Title "Select a Subscription" -PassThru).SubscriptionId
Select-AzureRmSubscription -SubscriptionId $subId
$vms = Get-AzureRmVM | Out-GridView -Title "Select VMs to delete. Use Crtl/Shift for multi selection." -PassThru
$deleteDisks = "No","Yes" | Out-GridView -Title "Do you wish to delete disks attached to the VM(s)?" -PassThru
$disksToDelete = @()
$nicToDelete = @()
foreach($vm in $vms)
{
$disksToDelete = Get-VMDisks($vm)
$nicToDelete = Get-VMNIC($vm)
Write-Host "Deleting VM:" $vm.Name -ForegroundColor Yellow
Remove-AzureRmVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force
Write-Host "Deleting NIC:" $nicToDelete -ForegroundColor Yellow
Remove-AzureRmNetworkInterface -Name $nicToDelete -ResourceGroupName $vm.ResourceGroupName -Force
if($deleteDisks -eq "Yes")
{
foreach ($disk in $disksToDelete)
{
Write-Host "Deleting Disk:" $disk -ForegroundColor Yellow
Delete-Disk($disk)
}
}
}
function Get-VMDisks($vm)
{
#Function to get all disks attached to a VM
$disks = @()
$disks += ,$vm.StorageProfile.OsDisk.Vhd.Uri
foreach ($disk in $vm.StorageProfile.DataDisks)
{
$disks += ,$disk.Vhd.Uri
}
return $disks
}
function Delete-Disk($uri)
{
#Function to get delete disk and delete the container if it is empty
$uriSplit = $uri.Split("/").Split(".")
$saName = $uriSplit[2]
$container = $uriSplit[$uriSplit.Length-3]
$blob = $uriSplit[$uriSplit.Length-2] + ".vhd"
$sa = Get-AzureRmStorageAccount | where {$_.StorageAccountName -eq $saName}
$saKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $sa.ResourceGroupName -Name $sa.StorageAccountName).Value[0]
$saContext = New-AzureStorageContext -StorageAccountName $sa.StorageAccountName -StorageAccountKey $saKey
Remove-AzureStorageBlob -Blob $blob -Container $container -Context $saContext
$remainingBlobs = Get-AzureStorageContainer -Name $container -Context $saContext | Get-AzureStorageBlob
if ($remainingBlobs -eq $null)
{
Remove-AzureStorageContainer -Name $container -Context $saContext -Force
}
}
function Get-VMNIC($vm)
{
$nic = $vm.NetworkInterfaceIDs.split("/") | Select-Object -Last 1
return $nic
}