So you’ve created a handy Azure function app with a function inside running your favorite PowerShell script. Now what? If you’ve created that function with an HTTP trigger as shown in the last tutorial in this series, it’s time to execute that script and see what happens!
By the end of this tutorial, you’ll know how to execute an Azure function written in PowerShell.
Prerequisites
- A pre-built Azure Function app with PowerShell function using an HTTP trigger
- Visual Studio (VS) Code setup with required extensions
- PowerShell v7+
This tutorial is #2 in the series we’re calling Building PowerShell Azure Functions. This tutorial assumes you already have an Azure function running PowerShell created with an HTTP trigger as demoed in the last tutorial.
Executing the Azure Functions PowerShell HTTP Trigger via the Azure Portal
There are a few different ways to execute an Azure function that uses an HTTP trigger. You can use the Azure Portal or pick any method you’d typically call an HTTP method like Postman or PowerShell itself! The beauty of an HTTP trigger for a PowerShell script is the ubiquitous ways to run it.
Assuming you have a function already built:
1. Head over to the Azure Portal and open the function app. You’ll see a Functions link, as shown below.
2. Click Functions. You should see a function called ExecutePowerShellScriptonHTTPEvent if you followed the previous tutorial in this series.
If you’re bringing your own Azure function, as long as it’s a PowerShell function triggered via HTTP, the following steps will still work.
3. Click on the function and click on the Code + Test item.
On the Code + Test screen, you’ll see the run.ps1 script that’s part of the function.
An Azure function built for PowerShell consists of a few different files such as run.ps1, the main executing script, requirements.psd1 for defining PowerShell module prerequisites, and profile.ps1 to execute PowerShell when the function app starts up.
The code in run.ps1 in the example above is the HTTP trigger template code provided by Microsoft. This code doesn’t do much for now but will provide some output to understand the execution.
4. Click on the Test/Run button where a handy HTTP blade pops up, allowing you to specify the method, query, and body you’d like to pass to the function.
By default, HTTP-triggered functions allow for HTTP GET and POST methods. You can add or remove allowed HTTP methods by changing the function.json file inside of the function along with other parameters outside the scope of this tutorial.
When you create an Azure function via VS Code, the function will be read-only inside of the Azure Portal. To make any changes, you must modify any files locally on your PC and deploy the changes to Azure.
5. For now, simply click on Run without providing any other information to see what happens. You should see the function run successfully, indicated by an HTTP 200/OK response code.
The text you see is generated via an output binding returned as the HTTP body using the Push-OutputBinding cmdlet inside the run.ps1 script, as shown below.
Notice there’s a Write-Host reference in the run.ps1 script above. Console output is returned via the log stream and is where you can see all internal logging information and any output the run.ps1 script sent.
6. Now, click on Test/Run again, and this time change the HTTP method to GET and add a Query name of name. If you’re using this series’ original function, the run.ps1 script has code that handles the name query parameter, as shown below.
You’ll now see that the function returns HTTP/200, but the body was changed based on the query value.
Executing the Azure Functions PowerShell HTTP Trigger via PowerShell
Since HTTP triggers attached to Azure functions provide a standardized HTTP interface, you can execute and test your Azure PowerShell functions with any method capable of sending HTTP requests, including PowerShell!
PowerShell has two different cmdlets that make HTTP requests; Invoke-WebRequest and Invoke-RestMethod. Since our Azure function is essentially a little API at this point returning JSON, the Invoke-RestMethod will be the best bet for testing.
1. To execute the Azure function with PowerShell, you must get the endpoint to query. To do that, open the function, click on Code + Test, and then on Get function URL.
You’ll see an option for selecting a Key. This key comes from the authentication level when you create the Function App. Authentication is out of scope for this tutorial, but if you’d like to dig deeper into this topic, check out Azure Functions HTTP – Authorization Levels.
You can find all of the API
2. Keep the Key to default and copy the URL. This action will copy the endpoint to query along with the API key to authenticate to the function.
3. Open PowerShell on your local computer, run the Invoke-RestMethod cmdlet, and pass the URL you just copied. The tutorial’s demo function URL is provided below.
Invoke-RestMethod -Uri 'https://powershellfunctionsdemo.azurewebsites.net/api/ExecutePowerShellScriptonHTTPEvent?code=<My API Key here>'
You’ll see the same output the Azure Portal returned.
4. Now, pass in an HTTP query by including the key/value pair inside of the URL, making the URL now https://powershellfunctionsdemo.azurewebsites.net/api/ExecutePowerShellScriptonHTTPEvent?name=myname&code=<My API key here>
Again, you’ll see the same output as before in the Azure Portal but now in your PowerShell console.
Giving your Azure PowerShell functions an HTTP trigger allows you to execute the script from many different sources, including code and other tools.
Summary
This tutorial taught you how to execute a simple PowerShell Azure function. If you’ve followed both tutorials in this series, you should now be able to create and execute PowerShell Azure Functions and take your PowerShell scripts to the cloud!