Nobody likes wastage, but it happens. Finding orphaned Azure disks is super simple using the Cloud Shell.
In this post I show how you can use the CLI, with a little Bash scripting in the Cloud Shell, to find unattached disks and also tag them for future removal. There are plenty of PowerShell examples in the PowerShell Runbook gallery, but I want to show a CLI example. I then go on to show how this could be scheduled, but it’s not as simple as first thought.
Using Bash Cloud Shell & the Az Command (CLI)
We can use the az command (CLI) in a bash Cloud Shell to list the disks, then use JMESPath to filter and find the ones that are not attached (“Unattached”) to any VM:
az disk list --query "[?diskState=='Unattached'].{name:name,state:diskState,size:diskSizeGb,sku:sku.name,tag:to_string(tags)}" -o tsv
ase01_datadisk_2 Unattached 256 Standard_LRS null
ase01_OsDisk_1_8e31587ee6604463ada5167f91e6345f Unattached 30 StandardSSD_LRS null
Notice I have also included the “tags” column with some processing. If you wanted to search for disks that are not attached, and filter by tag, then you can do the following. First I apply a tag; I’m going to create a tag called “testTag”, with a value “testTagValue”:
az disk update --name ase01_datadisk_2 --resource-group UK-West --set tags.testTag=testTagValue
Now I have set a value for “testTag”, let’s query based on that specific tag, for “Unattached” disks:
az disk list --query "[?diskState=='Unattached'&&tags.testTag=='testTagValue'].{name:name,state:diskState,size:diskSizeGb,sku:sku.name,tag:to_string(tags)}" -o tsv
ase01_datadisk_2 Unattached 256 Standard_LRS {"testTag":"testTagValue"}
You can search for a specific value, contains a part of a value, or alternatively, search for a value that is “null”.
Adjusting the Disks
Now we have a list of disks output from our query, we could just delete them. But it’s probably better to tag them for deletion, allowing a deletion at some point after a more detailed review. I would look to produce a report that could go to a CAB review, ready for deletion.
Here’s how we can use the power of the Cloud Shell, the Azure CLI and Bash scripting to tag those disks that are Unattached:
set updated=0
set failed=0
set tabchar="$(printf "\t")"
set deldate="$(date -d "+ 30 days" "+%Y%m%d")"
az disk list --query "[?diskState=='Unattached'].{name:name,rg:resourceGroup}" -o tsv | while read line
do
diskname="${line%%${tabchar}*}"
rgname="${line##*${tabchar}}"
echo -n "Updating: $diskname ... "
az disk update --name ${diskname} --resource-group ${rgname} --set tags.deleteDate=${deldate} >/dev/null
if [[ $? -eq 0 ]] ; then
echo "[SUCCESS]"
(( updated++ ))
else
echo "[FAILED]"
(( failed++ ))
fi
done
printf "### SUMMARY ### \n Updated: %s\n Failed: %s\n" $updated $failed
Let’s look at the above script in detail below:
We define the variable to hold our updated count. We define the variable to hold our failed count. We execute the CLI query to get the list of disks, outputting the disk name and resource group into a “do” loop a line at a time. The start of the loop body. We have to capture the character that represents a TAB character. We get the disk name out of the line by splitting the line by TAB from the left. We get the disk resource group out of the line by splitting the line by TAB from the right. A little output text to say which disk is being worked on. The call to the CLI to update the disk, applying the tag “deleteDate” with a value of today + 30 days in the format yyyymmdd. We detect the successful (or not) execution of the CLI command. Output “success” for a successful execution. Update the success variable count. Alternatively, if we failed. Output “failed” for a failed execution. Update the failed variable count. Closure of the “if” statement. Closure of the loop. Output a summary count of success vs failed.
Here is the execution sample:
Reporting on the deleteDate Tag
Now we have our disks tagged ready for deletion, we might want to find disks that have a deleteDate older than a specific date. These disks would then be ripe for deletion. Here’s how we can do that,:
az disk list --query "[?tags.deleteDate<'20210402'].{name:name,rg:resourceGroup,tag:to_string(tags)}" -o tsv
We can even go as far as using Bash to inject the current date into the query, thereby allowing us to look for any disks due for deletion from today without adjusting the query each time:
az disk list --query "[?tags.deleteDate<'$(date "+%Y%m%d")'].{name:name,rg:resourceGroup,tag:to_string(tags)}" -o tsv
How About Scheduled Execution?
Well, using the CLI and the Bash shell inside an Azure Automation account is not possible right now. Instead we would need to convert our disk update scripted CLI example to PowerShell to make it Runbook compatible, then we can schedule inside an Azure Runbook as a PowerShell Runbook. The code needs to change slightly because we need to use the automation “RunAs Account” feature to connect from our Runbook, but it looks very much like our CLI code in Bash:
IMPORTANT : All the “simple” help guides on creating a PowerShell Runbook fail to mention the need to actually import the required PowerShell modules that you will need to run your code. In the Automation Account section, look at the “modules”. For the code below you need 2 additional modules: Az.Accounts and Az.Compute which you can import from the Gallery. Seems simple, but as I said, not obvious.
$Conn = Get-AutomationConnection -Name 'AzureRunAsConnection'
try{
$auth = Connect-AzAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint
} Catch {
throw $_.Exception
}
$updated=0
$failed=0
$deldate=(get-date -Format "yyyyMMdd" -date $([datetime]::parseexact($(get-date -format 'yyyyMMdd'), 'yyyyMMdd', $null)).AddDays(+30))
$updateConfig = New-AzDiskUpdateConfig -tag @{ "deleteDate" = "$deldate" }
Get-AzDisk |? { $_.DiskState -eq "Unattached" } |% {
write-output "Updating: $($_.Name) ..."
Update-AzDisk -ResourceGroupName $_.ResourceGroupName -DiskName $_.Name -DiskUpdate $updateConfig >$null 2>&1
if ( $? -eq $true ) {
echo " [SUCCESS]"
$updated++
}
else {
echo " [FAILED]"
$failed++
}
}
echo "### SUMMARY ### `n Updated: ${updated}`n Failed: ${failed}`n"
With the above code in a PowerShell Runbook, we can test it:
Obviously if you will be scheduling the code above, then you may wish to change it slightly so that it excludes any orphaned disks already with a deleteDate tag.
Summary
Using the CLI, Cloud Shell and some Bash scripting, we have a simple mechanism to tag unattached disks, then use that tag to report on disks due for deletion after a specific date (or we could use today’s date). This is a great solution for those with Bash shell scripting skills and as shown it is reasonably simple.
We have also looked at the possibility of scheduling the code and found that it is not possible for CLI. Instead a PowerShell Runbook is a possible solution that allows the scheduling of PowerShell code. PowerShell could be your pain point, but it really isn’t that far from shell .