简体   繁体   中英

Find/list the Unused storage accounts in azure using powershell

Trying to get the list of unused/inactive storage accounts in azure using powershell. Below is my script which im trying it will provide the storage account name and last modified date of your Azure storage accounts, but i need to list only the unused storage accounts names not all the storage accounts, for that some condition/filter i need to provide to achieve the same. Please assist me to solve this. Thanks in Advance

It will output the results into a table detailing the name and last modified date of your Azure storage accounts.

& {
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

在此处输入图像描述 Use get-date
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date

and use the Where-Object .

     # ADD THIS
 $lastModDate = (get-date).AddDays(-5).Date
 $lastMod = $lastModified | Where-Object { ($_.DateTime).Date -lt $lastModDate}
 
 # If $lastMod.DateTime is NOT empty, then:
 if ($lastMod.DateTime) { 
 
    # Write-Host "variable is NOT null " + $storageAccountName # For testing purpose
    # Collect the information to output to a table when the for loop has completed
    New-Object psobject -Property @{
        Name = $storageAccountName;
        LastModified = $lastMod.DateTime; # CHANGE THIS
        ResourceGroupName = $resourceGroupName
    }
 }

https://www.techielass.com/find-unused-storage-accounts-in-azure/

With your script:
在此处输入图像描述

With my changes:
在此处输入图像描述

I tried to reproduce the same in my environment and got the same result as below:

By using the same script, I got the storage account name and last modified date of the Azure storage accounts.

在此处输入图像描述

To get only the unused/inactive storage accounts in azure using PowerShell, I modified the script like below:

I agree with @ Niclas , you need make use of get-date command.

& {
  foreach ($storageAccount in Get-AzStorageAccount) {
    $storageAccountName = $storageAccount.StorageAccountName
    $resourceGroupName = $storageAccount.ResourceGroupName
    $storageAccountKey = (Get-AzStorageAccountKey -Name $storageAccountName -ResourceGroupName $resourceGroupName).Value[0]
    $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
    $lastModified = Get-AzStorageContainer -Context $context | Sort-Object -Property @{Expression = {$_.LastModified.DateTime}} | Select-Object -Last 1 -ExpandProperty LastModified
    $unusedacc = (Get-Date).AddDays(-10)
    if ($lastModified.DateTime -lt $unusedacc) {
        New-Object psobject -Property @{
        Name = $storageAccountName;
        LastModified = $lastModified.DateTime;
        ResourceGroupName = $resourceGroupName
      }
    }
  }
} | Format-Table Name, LastModified, ResourceGroupName -autosize

在此处输入图像描述

Note : Based on your requirement you can change the number of days in this line $unusedacc = (Get-Date).AddDays(-10) .

If there are no unused Storage accounts, then it will return blank results like below:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM