So that you’ve created a helpful Azure perform app with a perform inside working your favourite PowerShell script. Now what? Should you’ve created that perform with an HTTP set off as proven within the final tutorial on this collection, it’s time to execute that script and see what occurs!
By the top of this tutorial, you’ll know how one can execute an Azure perform written in PowerShell.
- A pre-built Azure Operate app with PowerShell perform utilizing an HTTP set off
- Visible Studio (VS) Code setup with required extensions
- PowerShell v7+
This tutorial is #2 within the collection we’re calling Constructing PowerShell Azure Capabilities. This tutorial assumes you have already got an Azure perform working PowerShell created with an HTTP set off as demoed within the final tutorial.
Executing the Azure Capabilities PowerShell HTTP Set off by way of the Azure Portal
There are a couple of alternative ways to execute an Azure perform that makes use of an HTTP set off. You should utilize the Azure Portal or choose any methodology you’d sometimes name an HTTP methodology like Postman or PowerShell itself! The fantastic thing about an HTTP set off for a PowerShell script is the ever-present methods to run it.
Assuming you could have a perform already constructed:
1. Head over to the Azure Portal and open the perform app. You’ll see a Capabilities hyperlink, as proven under.
2. Click on Capabilities. You must see a perform known as ExecutePowerShellScriptonHTTPEvent for those who adopted the earlier tutorial on this collection.
Should you’re bringing your individual Azure perform, so long as it’s a PowerShell perform triggered by way of HTTP, the next steps will nonetheless work.
3. Click on on the perform and click on on the Code + Take a look at merchandise.
On the Code + Take a look at display screen, you’ll see the run.ps1 script that’s a part of the perform.
An Azure perform constructed for PowerShell consists of some totally different information similar to run.ps1, the principle executing script, necessities.psd1 for outlining PowerShell module stipulations, and profile.ps1 to execute PowerShell when the perform app begins up.
The code in run.ps1 within the instance above is the HTTP set off template code supplied by Microsoft. This code doesn’t do a lot for now however will present some output to know the execution.
4. Click on on the Take a look at/Run button the place a helpful HTTP blade pops up, permitting you to specify the tactic, question, and physique you’d prefer to cross to the perform.
By default, HTTP-triggered capabilities enable for HTTP GET and POST strategies. You’ll be able to add or take away allowed HTTP strategies by altering the perform.json file within the perform together with different parameters exterior the scope of this tutorial.
While you create an Azure perform by way of VS Code, the perform might be read-only within the Azure Portal. To make any adjustments, you should modify any information regionally in your PC and deploy the adjustments to Azure.
5. For now, merely click on on Run with out offering another data to see what occurs. You must see the perform run efficiently, indicated by an HTTP 200/OK response code.
The textual content you see is generated by way of an output binding returned because the HTTP physique utilizing the Push-OutputBinding cmdlet contained in the run.ps1 script, as proven under.
Discover there’s a Write-Host reference within the run.ps1 script above. Console output is returned by way of the log stream and is the place you may see all inner logging data and any output the run.ps1 script despatched.
6. Now, click on on Take a look at/Run once more, and this time change the HTTP methodology to GET and add a Question identify of identify. Should you’re utilizing this collection’ authentic perform, the run.ps1 script has code that handles the identify question parameter, as proven under.
You’ll now see that the perform returns HTTP/200, however the physique was modified based mostly on the question worth.
Executing the Azure Capabilities PowerShell HTTP Set off by way of PowerShell
Since HTTP triggers connected to Azure capabilities present a standardized HTTP interface, you may execute and take a look at your Azure PowerShell capabilities with any methodology able to sending HTTP requests, together with PowerShell!
PowerShell has two totally different cmdlets that make HTTP requests; Invoke-WebRequest and Invoke-RestMethod. Since our Azure perform is basically somewhat API at this level returning JSON, the Invoke-RestMethod would be the greatest guess for testing.
1. To execute the Azure perform with PowerShell, you should get the endpoint to question. To try this, open the perform, click on on Code + Take a look at, after which on Get perform URL.
You’ll see an choice for choosing a Key. This key comes from the authentication stage once you create the Operate App. Authentication is out of scope for this tutorial, however for those who’d prefer to dig deeper into this subject, take a look at Azure Capabilities HTTP – Authorization Ranges.
You could find all the API
2. Hold the Key to default and replica the URL. This motion will copy the endpoint to question together with the API key to authenticate to the perform.
3. Open PowerShell in your native laptop, run the Invoke-RestMethod cmdlet, and cross the URL you simply copied. The tutorial’s demo perform URL is supplied under.
Invoke-RestMethod -Uri 'https://powershellfunctionsdemo.azurewebsites.web/api/ExecutePowerShellScriptonHTTPEvent?code=<My API Key right here>'
You’ll see the identical output the Azure Portal returned.
4. Now, cross in an HTTP question by together with the important thing/worth pair within the URL, making the URL now https://powershellfunctionsdemo.azurewebsites.web/api/ExecutePowerShellScriptonHTTPEvent?identify=myname&code=<My API key right here>
Once more, you’ll see the identical output as earlier than within the Azure Portal however now in your PowerShell console.
Giving your Azure PowerShell capabilities an HTTP set off lets you execute the script from many various sources, together with code and different instruments.
This tutorial taught you how one can execute a easy PowerShell Azure perform. Should you’ve adopted each tutorials on this collection, it’s best to now be capable of create and execute PowerShell Azure Capabilities and take your PowerShell scripts to the cloud!