David Gard
David Gard

Reputation: 12087

Get function URL's using AZ PowerShell

In PowerShell, I'm attempting to retrieve the invocation URL and key for a Function App. After several hours of trying to do just that, I've hit a brick wall in relation to the invocation URL. Is this actually possible?

As Microsoft haven't bothered to provide an equivalent to az rest, I've been looking at Invoke-AzResourceAction, the documentation for which reads as follows -

The Invoke-AzResourceAction cmdlet invokes an action on a specified Azure resource. To get a list of supported actions, use the Azure Resource Explorer tool.

After sometime using the Azure Resource Explorer tool I realised that it was not really very helpful, so I started looking for other options and found Get-AzProviderOperation

The Get-AzProviderOperation gets the operations exposed by Azure resource providers.

Using the above command I can see that there are three available operations -

Operation                                        OperationName                   ProviderNamespace  ResourceName       Description            IsDataAction
---------                                        -------------                   -----------------  ------------       -----------            ------------
microsoft.web/sites/functions/action             Functions Web Apps              Microsoft Web Apps Web App            Functions Web Apps.           False
microsoft.web/sites/functions/listsecrets/action List Web Apps Functions Secrets Microsoft Web Apps Web Apps Functions List Function secrets.        False
microsoft.web/sites/functions/listkeys/action    List Web Apps Functions Keys    Microsoft Web Apps Web Apps Functions List Function keys.           False

First attempt

Initially I was happy using the listsecrets action, but after adding a function with a custom route to my Function App I noticed that listsecrets does not honour this custom route in the trigger_url property, so I had to abandon use of it.

$functionApp = Get-AzWebAppSlot -Name $FunctionAppName -ResourceGroup $ResourceGroupName -Slot $Slot
$functionApp | Format-List
$functionKey = Invoke-AzResourceAction -ResourceId ("{0}/functions/{1}" -f $functionApp.Id, $FunctionName) -Action "listsecrets" -ApiVersion "2019-08-01" -Force

Second attempt

Next I tried to use the listkeys action to get the function key, in conjunction with functions action. I know for the REST API that the invocation URL can be found at properties.invoke_url_template when querying a function, so I was hoping for the same here. Alas...

Invoke-AzResourceAction -ResourceId ("{0}/functions" -f $functionApp.Id) -Action $FunctionName -ApiVersion "2019-08-01" -Force

No route registered for '/api/functions/Health?api-version=2019-08-01'

It's quite possible I'm implementing Invoke-AzResourceAction incorrectly in this case (never used it prior to today).

Other things I've thought about

In addition to lacking an equivalent to az rest, AZ PowerShell also lacks an equivalent to az-account-get-access-token. This means that I can't grab my token and simply query the REST API.

Upvotes: 4

Views: 3610

Answers (1)

Joy Wang
Joy Wang

Reputation: 42153

As I mentioned in the comment, if you want to get the function(trigger) url, including the function key, you could use the command below, the $FunctionURL is what you want.

$FunctionAppName = "joyfun"
$GroupName = "xxxxx"
$TriggerName = "HttpTrigger1"
$FunctionApp = Get-AzWebApp -ResourceGroupName $GroupName -Name $FunctionAppName
$Id = $FunctionApp.Id
$DefaultHostName = $FunctionApp.DefaultHostName
$FunctionKey = (Invoke-AzResourceAction -ResourceId "$Id/functions/$TriggerName" -Action listkeys -Force).default
$FunctionURL = "https://" + $DefaultHostName + "/api/" + $TriggerName + "?code=" + $FunctionKey

enter image description here

If the trigger is in a slot, change this line $FunctionApp = Get-AzWebApp -ResourceGroupName $GroupName -Name $FunctionAppName to $FunctionApp = Get-AzWebAppSlot -ResourceGroupName $GroupName -Name $FunctionAppName -Slot <slotname>.

enter image description here

Update:

I test the custom route on my side, my host.json snippet is like below.

{
  "extensions": {
        "http": {
            "routePrefix": "api/Health",
        }
    }
}

You could use the command below.

$FunctionAppName = "joyfun"
$GroupName = "xxxxx"
$TriggerName = "HttpTrigger1"
$FunctionApp = Get-AzWebApp -ResourceGroupName $GroupName -Name $FunctionAppName
$Id = $FunctionApp.Id
$DefaultHostName = $FunctionApp.DefaultHostName
$FunctionKey = (Invoke-AzResourceAction -ResourceId "$Id/functions/$TriggerName" -Action listkeys -Force).default
$invokeurl = (Get-AzResource -ResourceId "$Id/functions" | Where-Object {$_.Name -eq "$FunctionAppName/$TriggerName"}).Properties.invoke_url_template
$FunctionURL = $invokeurl + "?code=" + $FunctionKey

enter image description here

Upvotes: 2

Related Questions