I have a script that creates a Function App and creates a new Host Key. I then started trying to find out how I could get just that new Azure generated key out and into a PowerShell variable.
Although the az functionapp keys list
command brings back the keys, they are not in an array, but inconveniently as a JSON object and each key as a property of 'functionKeys'.
{
"functionKeys": {
"my-new-host-key": "t0vI5oSzLIC####################Uk9LszHMpJ4CR6SzxjIg==",
"default": "XNDw3Ywc0jKBD####################gB6sdRyenK1qdTd2g=="
},
"masterKey": "2QGJLDAEbPSO####################XE/n8er18vwyKWbowbA==",
"systemKeys": {}
}
This causes all sorts of pain when just trying to get the one you want into a variable.
After a lot of flapping around trying various JMESPath techniques and due to the transient nature of the actual key name, which is in a variable of it's own, I produced the following, but there must be a simpler way.
Have I missed something? Can anyone recommend a better and simpler solution than mine below?
$newHostKeyName = "my-new-host-key"
# Get the functionKeys JSON object and convert to PowerShell object
$allKeys = az functionapp keys list -g $ResourceGroupName -n $FunctionAppName --query 'functionKeys' | ConvertFrom-Json
# Create the command to now get the single proprty value from the PowerShell object
$getkeyCommandString = "`$allKeys.'" + $newHostKeyName + "'"
$getkeyScriptBlock = [Scriptblock]::Create($getkeyCommandString)
# Finally I can extract the key secret
$funcHostKeySecret = Invoke-Command -ScriptBlock $getkeyScriptBlock