Update : updated script to support Azure Az powershell module.
Azure storage accounts is Microsoft’s durable, highly available, scalable and secure storage solution on cloud. It helps storing flat files, media object, images, binary executable etc. in cloud.
Storage accounts come in different types and access tiers. Refer Microsoft documentation for detailed information on different storage account types. Today, I will just talk a little bit about storage account access tiers as the script I am providing today is to automate the storage account access tier change.
Microsoft provides three different access tiers based on storage access patterns :
- Hot – Optimized for storing frequently accessed data.
- Cool – Optimized for infrequently accessed data and stored for at least 30 days.
- Archive – Optimized for rarely access data and stored for at least 180 days with flexible latency requirements.
Every access tier comes with different pricing and it is always good to wisely distribute storage workload in different access tier based on their accessibility.
As part of my cost saving project, I started exploring our storage data and their access pattern. We do maintain a lot of static data which we hardly access for months or sometimes for years. It is really important to move to right access tier. After discussing with data custodians and stake holders we identified storage accounts and decided to move those to Cool access tier from Hot. I will not talk about life cycle policy today. It will be discussed in a different post soon.
Here is the script, to change the storage access tier for a list of storage accounts. Before executing the script, login to your azure account using Login-AzAccount command.
Make sure you have Azure Az Module installed and imported on the system before running this script. For more information on how to install and configure Az module refer following article : https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-5.5.0
Once you logged in, run the script provided below with parameters. Here is an example, .\Update-StorageAccountAccessTier.ps1 – saListPath c:\testpath\testfile.txt -currentAccessTier Hot -targetAccessTier Cool
Param(
[Parameter(Mandatory=$true)]
[String]$saListPath,
[Parameter(Mandatory=$true)]
[String]$currentAccessTier,
[Parameter(Mandatory=$true)]
[String]$targetAccessTier
)
$date = Get-Date -UFormat "%m-%d-%Y_%H%M%S"
$dFormat = "%m-%d-%Y %H:%M:%S"
$currentDir = $(Get-Location).Path
$tFile = "$($currentDir)\SAAccessTier_Transcript_$($date).txt"
$iFile = $saListPath
if(!(Test-Path $iFile)){
Write-Host "[$(Get-Date -UFormat $dFormat)] ERROR: Supplied Storage Account list file path is invalid. Provide correct file path and try again."
Exit
}
Start-Transcript -Path $tFile -Append -NoClobber
$currentSATier = $currentAccessTier
$targetSATier = $targetAccessTier
#region - run through the list of storage accounts and change access tier from Hot/Cool
foreach($sa in Get-Content $iFile){
$saDetails = $saResourceGroup = $saAccessTier = $saChange = ""
Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: Checking and changing Storage Account Access Tier for $sa" -ForegroundColor Green
$saDetails = Get-AzResource -Name $sa -ResourceType Microsoft.Storage/storageAccounts -ErrorAction SilentlyContinue
if([string]::IsNullOrEmpty($saDetails)){
Write-Host "[$(Get-Date -UFormat $dFormat)] ERROR: Coundn't find the Storage Account $sa" -ForegroundColor Red
}
else{
Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: Storage Account $sa found" -ForegroundColor Green
Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: Gathering required information for Storage Account $sa" -ForegroundColor Green
$saResourceGroup = $saDetails.ResourceGroupName
$saAccessTier = (Get-AzStorageAccount -ResourceGroupName $saResourceGroup -Name $sa -ErrorAction SilentlyContinue).AccessTier
Write-Host $saAccessTier
if($saAccessTier -eq $currentSATier){
Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: Changing Access Tier to $targetSATier for $sa" -ForegroundColor Green
$saChange = Set-AzStorageAccount -ResourceGroupName $saResourceGroup -AccountName $sa -AccessTier $targetSATier -Force -Confirm:$false
}
else{
Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: No Change is required as $sa is already in Access Tier - $targetSATier" -ForegroundColor Green
}
$saDetails = Get-AzStorageAccount -ResourceGroupName $saResourceGroup -Name $sa -ErrorAction SilentlyContinue
if($saDetails.AccessTier -eq $targetSATier){
Write-Host "[$(Get-Date -UFormat $dFormat)] INFORMATION: Access Tier has been changes successfully for $sa to $targetSATier" -ForegroundColor Green
}
else{
Write-Host "[$(Get-Date -UFormat $dFormat)] ERROR: Failed to change Access Tier for $sa to $targetSATier" -ForegroundColor Red
}
}
Write-Host (("=") * 100)
}
Stop-Transcript
#end-region