Skip to content

Instantly share code, notes, and snippets.

@weeyin83
Created January 20, 2022 14:34
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
This script queries your Azure subscription and gathers the name and last modified date of your Azure storage accounts.
<#
.SYNOPSIS
FindUnusedAzureStorage.ps1
.DESCRIPTION
This script queries your Azure subscription and gathers the name and last modified date of your Azure storage accounts.
.OUTPUTS
It will output the results into a table detailing the name and last modified date of your Azure storage accounts.
.NOTES
Written by: Sarah Lean
Find me on:
* My Blog: http://www.techielass.com
* Twitter: https://twitter.com/techielass
* LinkedIn: http://uk.linkedin.com/in/sazlean
.EXAMPLE
.\FindUnusedAzureStorage.ps1
This will query your Azure subscription looping round each storage account gather the name and last modified date. The output table will show you the storage account name, last modified date and the resource group that storage account is stored in. Please note that the last modified date is shown in MM/DD/YYYY format.
Change Log
V1.00, 20th January 2022 - Initial version
License:
The MIT License (MIT)
Copyright (c) 2022 Sarah Lean
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
#>
& {
foreach ($storageAccount in Get-AzStorageAccount) {
$storageAccountName = $storageAccount.StorageAccountName
$resourceGroupName = $storageAccount.ResourceGroupName
# Get storage account key
$storageAccountKey = (Get-AzStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
# Create storage account context using above key
$context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
# Get the last modified date
$lastModified = Get-AzStorageContainer -Context $context
| Sort-Object -Property @{Expression = {$_.LastModified.DateTime}}
| Select-Object -Last 1 -ExpandProperty LastModified
# Collect the information to output to a table when the for loop has completed
New-Object psobject -Property @{
Name = $storageAccountName;
LastModified = $lastModified.DateTime;
ResourceGroupName = $resourceGroupName
}
}
} | Format-Table Name, LastModified, ResourceGroupName -autosize
@fritztek
Copy link

Hi Sarah, when I run the script, I receive errors about the pipes in Lines 62, 63 and73. Removing the pipes allows the code to run but I only see the: storage account and resource group.

I do not get last modified date and export-csv yields no export.

Can you offer me some guidance?

@weeyin83
Copy link
Author

@fritztek How are you executing the script? I've just copied it and pasted it into an Azure CLI window and it has popped out the results I was looking for?

@fritztek
Copy link

Hi Weeyin83, Thank you for the quick response. I was executing the code in Visual Code. Executing in Azure CLi works but I do not get info on all of the storage accounts.

image
StorageAccountFailure

@fritztek
Copy link

I ran the script again on the same subscription and no longer get any dates
2022-08-19 16_32_20-Virtual machines - Microsoft Azure — Mozilla Firefox
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment