Introduction
In this blog, you will see how to retrieve and refresh Azure Storage Account Access Keys using PowerShell.
Prerequisites
PowerShell Script
Open Notepad and paste the below code. Save the file as script.ps1.
- ################# Azure Blob Storage - PowerShell ####################
-
- ## Input Parameters
- $resourceGroupName="azpractice"
- $storageAccountName="azstorageacc1122020"
-
- ## Connect to Azure Account
- Connect-AzAccount
-
- ## Function to retrive and refresh access keys
- Function RetriveRefreshKeys
- {
- Write-Host -ForegroundColor Green "Retrieving the storage accounts keys..."
-
- ## Get the storage account keys
- $storageAccColl=Get-AzStorageAccount
- $storageAccountKey1 = (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[0]
- $storageAccountKey2= (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[1]
-
- Write-Host -ForegroundColor Yellow "Storage Account Key 1: " $storageAccountKey1
- Write-Host -ForegroundColor Yellow "Storage Account Key 2: " $storageAccountKey2
-
- Write-Host -ForegroundColor Green "Refreshing the storage accounts key (2)..."
-
- ## Refresh the storage account key 2
- New-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName -KeyName key2
-
- ## Retrive storage account key 2
- $storageAccountKey2= (Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName).Value[1]
- Write-Host -ForegroundColor Yellow "Storage Account Key 2: " $storageAccountKey2
- }
-
- RetriveRefreshKeys
-
- ## Disconnect from Azure Account
- Disconnect-AzAccount
Open PowerShell window and navigate to the location where the script file was saved. Run the following command.
./script.ps1
Result
Summary
Thus, in this blog, you saw how to retrieve and refresh Azure Storage Account Access Keys using PowerShell.