Search code examples
powershellazure-functionsazure-storageazure-table-storageazure-keyvault

Accessing Azure Storage through keys managed by Azure Key Vault via Powershell


I am having trouble accessing my storage account keys in a managed key vault. Here is my code:

$secret = Get-AzKeyVaultManagedStorageAccount -VaultName $keyVaultName -Name $storageAccountName
$ctx =New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $secret.SecretValueText

It seems that $secret.SecretValueText is empty/null. How do I retrieve the storage account key correctly? This is the error that appears.

New-AzStorageContext : Cannot validate argument on parameter 'StorageAccountKey'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Solution

  • Definitely it will be empty, the output of Get-AzKeyVaultManagedStorageAccount is PSKeyVaultManagedStorageAccount, it does not have such a property, the SecretValueText is a property of PSKeyVaultSecret.

    And I don't think you can get the storage account key via Get-AzKeyVaultManagedStorageAccount, if you want to get the storage account key, you can use the command below, make sure your logged user account/service principal has the RBAC role e.g. Storage Account Key Operator Service Role, Contributor, Owner in your storage account.

    $key = (Get-AzStorageAccountKey -ResourceGroupName <group-name> -Name <storageaccount-name>).Value[0]
    New-AzStorageContext -StorageAccountName <storageaccount-name> -StorageAccountKey $key
    

    enter image description here