Azure – Get Storage Account Lifecycle Management Policy Rules

By Saad Khamis

October 12, 2023

54854 views

In this blog I will share one way to get Azure Storage Account Lifecycle Management Policy Rules.

What is Azure Storage Account Lifecycle Management Policy?

According to Microsoft, Azure Storage lifecycle management offers a rule-based policy that you can use to transition blob data to the appropriate access tiers or to expire data at the end of the data lifecycle. A lifecycle management policy is a collection of rules in a JSON document.

More information can be found in Configure a lifecycle management policy and Optimize costs by automatically managing the data lifecycle.

Prerequisites

  1. First thing to remember is to install Azure PowerShell. Otherwise, you can use Cloud Shell if you prefer to stay within Azure Portal.
  2. Secondly, connect to Azure using Connect-AzAccount.

PowerShell Cmdlets

  1. Connect-AzAccount Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
  2. Get-AzStorageAccountManagementPolicy Gets the management policy of an Azure Storage account.

Lifecycle Management Policy Rules for a Storage Account

  1. First we will identify a storage account by its name a its resource group.
  2. Second, we will use Get-AzStorageAccountManagementPolicy to get the storage account management policy.
  3. Last step is to get the policy rules.

Get Lifecycle Management Policy Rules for a Storage Account

$StorageAccount = "safunctionslogs"
$ResourceGroup  = "rg-infra"

# Gets the storage account management policy
Get-AzStorageAccountManagementPolicy 
        -ResourceGroupName $StorageAccount.ResourceGroupName 
        -StorageAccountName $StorageAccount.StorageAccountName

# Gets the storage account management policy rules
(Get-AzStorageAccountManagementPolicy 
        -ResourceGroupName $StorageAccount.ResourceGroupName 
        -StorageAccountName $StorageAccount.StorageAccountName 
).Rules

# Gets the storage account management policy rules' names
(Get-AzStorageAccountManagementPolicy 
        -ResourceGroupName $StorageAccount.ResourceGroupName 
        -StorageAccountName $StorageAccount.StorageAccountName 
).Rules.Name

Output of the last command if the storage has a lifecycle management policy:

PS C:\> (Get-AzStorageAccountManagementPolicy -StorageAccountName $StorageAccount -ResourceGroupName $ResourceGroup).Rules.Name
ru-safunctionslog_2023-01-26 04:03:35.786
ru-safunctionslog_2023-05-26 05:13:26.871
ru-safunctionslog_2023-10-01 15:33:28.740

Output of the last command if the storage does not have a lifecycle management policy:

Get-AzStorageAccountManagementPolicy : No ManagementPolicy found for account safunctionslogs
At line:1 char:2
+ (Get-AzStorageAccountManagementPolicy -StorageAccountName $StorageAcc ...
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzStorageAccountManagementPolicy], CloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Management.Storage.GetAzureStorageAccountManagementPolicyCommand

Get Lifecycle Management Policy Rules for All Storage Accounts

  1. First, we will get all existing Azure storage accounts.
  2. Second, For each storage account, we will get lifecycle management policy.
  3. Last, we will get rules for the storage account lifecycle management policy.

Note the usage of -ErrorAction SilentlyContinue to suppress displaying errors when the storage account does not have a lifecycle management policy.

# Get all existing storage accounts
$StorageAccounts = Get-AzStorageAccount | Sort-Object StorageAccountName

# For each storage account
ForEach ($StorageAccount in $StorageAccounts) {
    Write-Host $StorageAccount.StorageAccountName -ForegroundColor Green
    # Get lifecycle management policy rules
    (Get-AzStorageAccountManagementPolicy 
            -ResourceGroupName $StorageAccount.ResourceGroupName 
            -StorageAccountName $StorageAccount.StorageAccountName 
            -ErrorAction SilentlyContinue
    ).Rules.Name
}

Import Lifecycle Management Policy Rules for All Storage Accounts to CSV File

Connect-AzAccount

$JoinChar = ", "

$StorageAccounts = Get-AzStorageAccount | Sort-Object StorageAccountName

Write-Host ("")
$Output = @() # Initialize output
ForEach ($StorageAccount in $StorageAccounts) {
    
    # Initialize $Result object
    $Result = "" | Select StorageAccount, ResourceGroup, Rules
    
    # Save storage account name and its resource group
    $Result.StorageAccount = $StorageAccount.StorageAccountName
    $Result.ResourceGroup  = $StorageAccount.ResourceGroupName

    # Get lifecycle management policy 
    $Policy = Get-AzStorageAccountManagementPolicy 
                    -ResourceGroupName $StorageAccount.ResourceGroupName 
                    -StorageAccountName $StorageAccount.StorageAccountName 
                    -ErrorAction SilentlyContinue
    If ($Policy) { # Policy exists
        Write-Host ("$($StorageAccount.StorageAccountName) ($($StorageAccount.ResourceGroupName)):") -ForegroundColor Green
        # convert rules to a string and save them
        $Result.Rules = $Policy.Rules.Name -join $JoinChar | Out-String
        <#
        # Display existing rules, if desired
        ForEach ($Rule in $Policy.Rules) {
            Write-Host ("`tRule Name: $($Rule.Name)")
        }
        #>
    }
    Else {
        Write-Host ("$($StorageAccount.StorageAccountName) ($($StorageAccount.ResourceGroupName)): No ManagementPolicy found") -ForegroundColor Yellow
        $Result.Rules = "No Management Policy found"
    }
    $Output += $Result
}
Write-Host ("")
$Output | Export-Csv -NoTypeInformation C:\Temp\saLifeCycleMgmtRules.csv

Conclusion

Did you find this blog easy to follow and helpful to you? Let me know in the comments below.

Disclaimer

Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments

There's no comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

List all AWS Certificate Manager certificates

By Saad Khamis 126904 views July 31, 2024

Get all AWS EBS snapshots across all profiles and regions

By Saad Khamis 131816 views June 26, 2024

AWS IAM role is not listed in the IAM roles dropdown menu for EC2

By Saad Khamis 130112 views June 25, 2024