Azure Functions using PowerShell
  • 09 Nov 2020
  • 2 Minutes To Read
  • Contributors
  • Print
  • Comments
  • Share
  • Dark
    Light

Azure Functions using PowerShell

  • Print
  • Comments
  • Share
  • Dark
    Light

#ServerlessTips - Azure Functions
Author: Dave Rendón Microsoft Azure MVP

On this article, we will demonstrate how to work with Azure Functions using PowerShell.

PowerShell is a great language for automating tasks, and now with the support in Azure Functions, you can automate operational tasks and take advantage of the native Azure integration to modernize the delivering and maintenance of services.

Benefits of Azure Functions using PowerShell

• Integration with the Azure Portal and Visual Studio Code
• Support for hybrid environments
• Integration with Azure Monitor
• Support for multiple plans (consumption-based, Premium, App Service Plan)

Pre-requisites

• An active azure subscription
• PowerShell 7.0

Create PowerShell Azure Function

Go to the Azure Portal and create a new Azure Function. Click on Add new resource and look for Function App as shown below:

image.png

Now, click create:

image.png

Now, create a new Resource Group and provide the parameters to create the Azure Function. Note that we will create a new resource group and select the publish method as ‘Code’, then select the runtime stack as ‘PowerShell Core’ and version ‘7.0’.

image.png

Now, in the Hosting tab, create a new storage account and select the ‘Plan Type’ as ‘consumption’ as shown below:

image.png

Note: There are 3 plan types, select 'Consumption' for dynamic scaling and on-demand execution, 'Premium' for advanced features and better control over dynamic scaling, or 'App Service Plan' for on-demand execution.

In the monitoring tab, you can enable Application Insights, in this case, we will enable this feature and will create the Application Insights in the same region we have been working with, which is Central US:

image.png

Optionally you can apply a tag for the Azure Function, then click on Review + Create.

image.png

Now, review the details and then click Create:

image.png

After a few minutes you should see your Azure Function created. Click on ‘Go to resource’:

image.png

Now, in the Azure Function, click on the 'App files' and you will see 3 files listed as shown below:

  • host.json:
  • profile.ps1:
  • requirements.psd1

image.png

Azure provides a feature that provides automatic management of the Az modules; this means that Azure modules will be natively available for your scripts so you can manage services available in Azure without having to include these modules with each Function created. Critical and security updates in these Az modules will be automatically upgraded by the service when new minor versions are released.

By default this feature is enabled for you, ensure that in the 'host.json' file the setting 'managedDependency' is set to true and ensure that modules are automatically managed by the Functions service in the ‘requirements.psd1’ to include Az Module as shown below:

Host.json File:

image.png

Requirements.psd1 file:

image.png

Create PowerShell Azure Function

Go to the Functions option and click add.

image.png

Now, select the ‘HTTP trigger’ and click OK.

image.png

Now, provide a name for the Azure Function and the authorization level, then click on ‘Create Function’.

image.png

Once your Azure Function is created, you should see the ‘Code + Test’ option 2 files:

  • function.json
  • run.ps1

image.png

Now, click on Test/Run and execute this test with the default parameters:

image.png

Note: The first managed dependency download will be executed; function execution will continue when it's done. Depending on the content of requirements.psd1, this can take a few minutes.

After a few minutes you should see a notification similar to the below:

image.png

From this moment you can write/modify your PowerShell Azure Function according to your needs.

Along with this article, we reviewed the benefits of working with Azure Functions using PowerShell and how you create your PowerShell Azure Function through the Azure Portal.

Was This Article Helpful?