Azure – Get Storage Account Lifecycle Management Policy Rules
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
- First thing to remember is to install Azure PowerShell. Otherwise, you can use Cloud Shell if you prefer to stay within Azure Portal.
- Secondly, connect to Azure using Connect-AzAccount.
PowerShell Cmdlets
- Connect-AzAccount Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
- Get-AzStorageAccountManagementPolicy Gets the management policy of an Azure Storage account.
Lifecycle Management Policy Rules for a Storage Account
- First we will identify a storage account by its name a its resource group.
- Second, we will use Get-AzStorageAccountManagementPolicy to get the storage account management policy.
- Last step is to get the policy rules.
Get Lifecycle Management Policy Rules for a Storage Account
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$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:
1 2 3 4 |
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:
1 2 3 4 5 6 |
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
- First, we will get all existing Azure storage accounts.
- Second, For each storage account, we will get lifecycle management policy.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# 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
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 |
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.
There's no comments