How to use Get-AzKeyVaultSecret in Powershell Azure Function 2.x

How to use Get-AzKeyVaultSecret in Powershell Azure Function 2.x

Taken from Microsoft: https://docs.microsoft.com/en-us/azure/key-vault/quick-create-powershell

Have you tried dot notation on retrieving those keys?

(Get-AzKeyVaultSecret -vaultName $VaultName -name $KeyName).SecretValueText

If that doesnt work, you may look at this github issue regarding ManagedAppServices: https://github.com/Azure/azure-powershell/issues/8983

Seems to be the same issue youre having.

There is now a new method for this.

$secret = Get-AzKeyVaultSecret -VaultName <your-unique-keyvault-name> -Name ExamplePassword
$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secret.SecretValue)
try {
  $secretValueText = [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr)
} finally {
  [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}
Write-Output $secretValueText

Refer to: https://docs.microsoft.com/en-us/azure/key-vault/secrets/quick-create-powershell

How to use Get-AzKeyVaultSecret in Powershell Azure Function 2.x

The original error is fixed in Azure Functions now.

It is still necessary to retrieve keys that are updated frequently in key vault. To do that, I use this:

(Get-AzKeyVaultSecret MyVaultName -Name MySecretName).secretvalue | ConvertFrom-SecureString -AsPlainText

Leave a Reply

Your email address will not be published.