0

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
NER1808
  • 1,829
  • 2
  • 33
  • 45

1 Answers1

1

You don't need a scriptblock or Invoke-Command just to get the value you seek from the returned JSON.

Just do:

$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

$hostKey = $allKeys.functionKeys.$newHostKeyName  # --> t0vI5oSzLIC####################Uk9LszHMpJ4CR6SzxjIg==

Or direct without using a variable $newHostKeyName:

$hostKey = $allKeys.functionKeys.'my-new-host-key'
Theo
  • 57,719
  • 8
  • 24
  • 41