Clean up orphaned Azure resource manager disks
Firstly, let me apologise, this post isn’t about automatically cleaning up disks in Azure Resource Manager storage accounts, instead it’s about obtaining the information which you can use to understand which disks are orphaned and can or should be deleted.
When deleting a virtual machine on its own, I’m sure you’re aware that the disks are not automatically deleted as part of that process. The recommended best practice is to put every resource that shares the same lifecycle in to the same resource group which can then be deleted as a single entity, removing virtual machines, storage accounts, NICs, load balancers etc in one hit.
In some cases, there are occasions where it’s preferable to delete a single virtual machine on its own – doing this leaves orphaned disks behind in your storage accounts, gathering dust (and costing money!) unless you manually delete the disks yourself. We all know that as we stand up and delete VMs in Azure, there are occasions where cleaning up just isn’t convenient and the disks get left and forgotten about. Many moons later we decide it’s a good time to clean up those orphaned disks but finding them manually or using Storage Explorer is a painful process when there’s potentially hundreds of storage accounts in a subscription.
Now that Managed Disks have gone Generally Available, this problem will occur less and less for people using those but for now, most people would benefit from understanding their VHD/storage account layout and usage.
I wrote the following PowerShell script to iterate over all (or some of the) storage accounts in a subscription, pulling out the details of any “blob” in any container, ending with .vhd. Rather than trying to identify unleased blobs and automatically deleting them (which is very dangerous when you have Azure Site Recovery protected objects) I chose to simply generate an object containing the information of every blob (ending with .vhd). For those of us happy using Powershell for filtering, we can then filter the object as we see fit or just output to CSV and filter in Excel for example.
You can target this to all storage accounts, some (via a regex match or like condition) or a specific account by adjusting the Get-AzureRmStorageAccount switches which act as the primary filtering mechanism. This script is non-destructive, it DOES NOT delete anything.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
<# Three options for using this script. 1. Filter by storage account, specifying the RG and SA name - quick 2. Filter using a match in a Where clause - moderate 3. No filter. Gets all VHDs in all storage accounts in the subscription - slow #> Login-AzureRmAccount # You may need or want to Select-AzureRmSubscription # Everything - SLOW $AllStorageAccounts = Get-AzureRmStorageAccount # Filter using a match (or a like) - MODERATE $AllStorageAccounts = Get-AzureRmStorageAccount | Where {$_.StorageAccountName -match '(accounts)'} # Filter by single SA - QUICK $AllStorageAccounts = Get-AzureRmStorageAccount -ResourceGroupName 'myresourcegroup' -Name 'mystorageaccount' $AllVHDs = $AllStorageAccounts | Get-AzureStorageContainer | Get-AzureStorageBlob | Where {$_.Name -like '*.vhd'} $Uri = foreach ($VHD in $AllVHDs) { $StorageAccountName = if ($VHD.ICloudBlob.Parent.Uri.Host -match '([a-z0-9A-Z]*)(?=\.blob\.core\.windows\.net)') {$Matches[0]} $StorageAccount = $AllStorageAccounts | Where { $_.StorageAccountName -eq $StorageAccountName } $Property = [ordered]@{ Uri = $VHD.ICloudBlob.Uri.AbsoluteUri; AttachedToVMName = $VHD.ICloudBlob.Metadata.MicrosoftAzureCompute_VMName LeaseStatus = $VHD.ICloudBlob.Properties.LeaseStatus; LeaseState = $VHD.ICloudBlob.Properties.LeaseState; StorageType = $StorageAccount.Sku.Name; StorageTier = $StorageAccount.Sku.Tier; StorageAccountName = $StorageAccountName; StorageAccountResourceGroup = $StorageAccount.ResourceGroupName } New-Object -TypeName PSObject -Property $Property } $Uri | Export-Csv -Path '.\DiskLeaseInformation.csv' -NoTypeInformation |
In my own tests, operating against ~90 storage accounts in a single subscription, retrieving data on ~600 VHD objects, the information was populated in to the object and a CSV created in ~50 seconds. You can of course include additional information in the object by adjusting the object properties accordingly but I would highly recommend that you do not use any cmdlets inside the foreach loop, instead get the source information outside of the loop and then filter for that information from the object inside the foreach, like I do with $StorageAccount.
You can use the CSV to identify objects that are surplus to requirements, badly named, in the wrong account/type and act on the information as you desire, but don’t be tempted to auto-delete anything from its output unless you’re supremely confident you know what you’re doing and especially not if you use Azure Site Recovery to protect on-premises machines…doing so is a Bad Idea™
Before I disappear and as a small bonus for reading this far, in my quest to gather this information elegantly, and before I properly investigated the ICloudBlob object’s properties, I also put together the following function which, given an HTTPS endpoint and the storage account key, uses the Azure Storage API to get the properties of a blob as response headers from an HTTP request. If nothing else it shows how to interact with the Storage API via WebRequests in PowerShell and construct an authenticated request.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Function Get-AzureRmBlobProperties { # See: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/get-blob-properties param( [parameter(Mandatory=$true)][uri] $Url, [parameter(Mandatory=$true)] [ValidateScript({ Try { [System.Convert]::FromBase64String($_) } Catch [FormatException] { Throw 'Possible bad storage account key specified. '+$_} })] [string] $StorageAccountKey ) $Method = "HEAD" $APIDate = '2014-02-14' $Headers = @{} $Headers.Add("x-ms-version",$APIDate) $Headers.Add("x-ms-date",$((Get-Date -Format r).ToString())) $StorageAccountName = If ($Url -match '([a-z0-9A-Z]*)(?=\.blob\.core\.windows\.net)') {$Matches[0]} Else {Throw 'Unparseable storage account name in the provided Url.'} $authString = "$Method$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)" $authString += "x-ms-date:" + $Headers["x-ms-date"] + "$([char]10)" $authString += "x-ms-version:" + $Headers["x-ms-version"] + "$([char]10)" $authString += "/" + $StorageAccountName + $Url.AbsolutePath $HMAC = New-Object System.Security.Cryptography.HMACSHA256((,[System.Convert]::FromBase64String($StorageAccountKey))) $Signature = [System.Convert]::ToBase64String($HMAC.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($authString))) $Headers.Add("Authorization", "SharedKey " + $StorageAccountName + ":" + $Signature); Return (Invoke-WebRequest -Uri $Url -Method $Method -Headers $Headers).Headers } Get-AzureRmBlobProperties -Url 'https://mystorageaccount.blob.core.windows.net/vhds/mydisk.vhd' -StorageAccountKey 'xxxxxx==' |
As a quick update, the above function can also be updated to easily become a replacement for Get-AzureStorageBlobContent by changing the method to GET and updating the return object. Why would you do this? Well, in my scenario I might be using Azure Automation and don’t want to download a file to then open it and read its contents. Get-AzureStorageBlobContent insists on downloading the file and I don’t want it to do that, the below function uses the Storage REST API to get a file’s contents as an object – obviously not something you’d do with a VHD but a CSV or JSON block blob for example isn’t large, is parseable and sits in an object nicely.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
Function Get-AzureRmBlob { param( [parameter(Mandatory=$true)][uri] $Url, [parameter(Mandatory=$true)] [ValidateScript({ Try { [System.Convert]::FromBase64String($_) } Catch [FormatException] { Throw 'Possible bad storage account key specified. '+$_} })] [string] $StorageAccountKey ) $Method = "GET" $APIDate = '2014-02-14' $Headers = @{} $Headers.Add("x-ms-version",$APIDate) $Headers.Add("x-ms-date",$((Get-Date -Format r).ToString())) $StorageAccountName = If ($Url -match '([a-z0-9A-Z]*)(?=\.blob\.core\.windows\.net)') {$Matches[0]} Else {Throw 'Unparseable storage account name in the provided Url.'} $authString = "$Method$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)$([char]10)" $authString += "x-ms-date:" + $Headers["x-ms-date"] + "$([char]10)" $authString += "x-ms-version:" + $Headers["x-ms-version"] + "$([char]10)" $authString += "/" + $StorageAccountName + $Url.AbsolutePath $HMAC = New-Object System.Security.Cryptography.HMACSHA256((,[System.Convert]::FromBase64String($StorageAccountKey))) $Signature = [System.Convert]::ToBase64String($HMAC.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($authString))) $Headers.Add("Authorization", "SharedKey " + $StorageAccountName + ":" + $Signature); Return (Invoke-WebRequest -Uri $Url -Method $Method -Headers $Headers -ErrorAction Stop) } |
-Lewis