How to create a PowerShell Azure Function using the Azure Portal – 2020 edition

Microsoft PowerShell is actually a fairly advanced programming language that is used by both developers and administrators, often for doing administrative and automation tasks. Still, you can also write quite complicated code in PowerShell. When you use Azure Functions, you traditionally use a more mainstream programming language like JavaScript or C#. But the Azure AppServices team found out that PowerShell is a pretty efficient language for writing Azure functions, probably because the nature of the PowerShell language encourages people to write smaller self-contained programs.

PowerShell in Azure Functions has been available for some time. But recently, there have been some updates to the Azure Portal UI and more advanced features like durable functions and support for the latest version of PowerShell Core. In this blog post, I’m looking into how to create a Function in the Azure Portal that utilizes PowerShell and how to edit and test it using the new Azure Portal Functions experience.

Create a PowerShell Function App in the Azure Portal

The easiest way to create an Azure Function is to go into the Azure Portal: https://portal.azure.com

In the search bar in the Azure Portal, write “functions,” and then you select “function app.” In there, you click on the little “+ add“-button in the upper left corner:

Now you’re filling in all the basic information, like the Resource Group and the name of the Function. In the “Publish” column, use select “Code,” and in the “Runtime Stack”-dropdown menu, you select “PowerShell Core.” In the version field, you can currently only select 7.0:

NOTE

Previously, you could select an earlier version of PowerShell Core, but this is now deprecated, and the only version that you can use for creating new PowerShell functions is 7.0.

If you already have PowerShell Functions that use a version other than 7.0 (like version 6), you can still use them for the foreseeable future, but if you want to create a new one, you have to choose PowerShell Core 7.

After you fill in all the information, click on the “Next: Hosting” button. This directs you to the “Hosting”-tab:

In the “Storage” section, you can select a Storage Account that store the actual Function app. Here we’re going to create a new Storage Account, but you can use an existing one as well.

In the “Operating System” section, you can only select “Windows.” When you create a PowerShell Core function like this, then the underlying operating system that runs behind the scenes in the function runtime will be Windows. And you cannot select  Linux for this particular type of Function.

When you choose a “Plan,” you can either choose to use the “Consumption (Serverless)” plan, or you can use the more enterprise-friendly Premium plan, and finally, you can host your Function inside an AppService Plan. In this blog post, we’ll go with the Consumption Plan.

Now click the bottom “Next: Monitoring.”  Inside the Monitoring tab, you can attach your Function to an Application Insights instance. Application Insights is a so-called application performance monitoring (APM) tool, and it is used for gaining insights into a running web application. You can then see exceptions and activity logs and metrics for your running web application. Whether you want to use Application Insights or not is it totally up to you,  and if you don’t want to use it right now, you can always add it later:

In this case, we’re not adding application insights, so we just put a checkmark in “No,” and then we will click on the button called “Review+Create.” If everything looks good, then just click “Create” and wait a few minutes for the PowerShell function to deploy:

Creating and editing PowerShell Function code

Open your newly created Azure Function app and click on “Functions” in the left-side menu and the click “+ Add”:

Inhere you can select the type of function you will create based on a set of templates. In this case, we select “HTTP trigger”:

You then give a name to your new function and select an “Authorization Level”. In this case, you select “Function” which means that the function itself will authenticate requests using an API-key. Finally, click “Create Function“:

It only takes a few seconds for Azure to create your function. When done, click “Code + Test” on the left-side menu.

In here you can edit, save, and test your new PowerShell function:

The “Test/Run” button at the top of the editor will allow you to test your function code directly in the Azure Portal.

The “Get Function URL” will create an URL for you that contains the API-keys of the function. With this URL you can call your function from everywhere.

Happy PowerShell coding with Azure Functions!

14