Private/Get-StorageAccountKey.ps1

function Get-StorageAccountKey {
    <#
    .SYNOPSIS
        Retrieves the primary access key for an Azure Storage Account.
    .PARAMETER AccountName
        Name of the storage account.
    .OUTPUTS
        [string] The primary access key.
    #>

    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory)]
        [string]$AccountName
    )

    Write-Host "[$(Get-Timestamp)] Retrieving key for storage account '$AccountName'..." -ForegroundColor Cyan
    $keysJson = az storage account keys list --account-name $AccountName --query '[0].value' -o tsv 2>&1
    if ($LASTEXITCODE -ne 0) {
        throw "Failed to retrieve keys for '$AccountName'. Ensure it exists and you have permissions.`n$keysJson"
    }
    $key = ($keysJson | Where-Object { $_ -isnot [System.Management.Automation.ErrorRecord] }) -join ''
    Write-Host " ✓ Key retrieved for '$AccountName'." -ForegroundColor Green
    return $key
}