Azure Functions using PowerShell
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)
• 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:
Now, click create:
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’.
Now, in the Hosting tab, create a new storage account and select the ‘Plan Type’ as ‘consumption’ as shown below:
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:
Optionally you can apply a tag for the Azure Function, then click on Review + Create.
Now, review the details and then click Create:
After a few minutes you should see your Azure Function created. Click on ‘Go to resource’:
Now, in the Azure Function, click on the 'App files' and you will see 3 files listed as shown below:
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:
Create PowerShell Azure Function
Go to the Functions option and click add.
Now, select the ‘HTTP trigger’ and click OK.
Now, provide a name for the Azure Function and the authorization level, then click on ‘Create Function’.
Once your Azure Function is created, you should see the ‘Code + Test’ option 2 files:
Now, click on Test/Run and execute this test with the default parameters:
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:
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.