How To Create PowerShell Azure Function

How To Create PowerShell Azure Function

In this Azure tutorial, we will discuss How to create PowerShell Azure Function. Along with this, we will also discuss a few other topics like What are the Benefits of Azure Functions using PowerShell, Azure Functions PowerShell Examples. Create PowerShell Azure Function Visual Studio Code, Run PowerShell Azure Function Locally, Deploy PowerShell Azure Function From Visual Studio Code and we will also discuss How To Create A PowerShell Azure Function Using The Azure Portal, Azure HTTP PowerShell Function App, Azure Function Run PowerShell Script and Create A PowerShell Timmer Trigger Azure Function Azure Portal, Azure Function PowerShell Queue Trigger.

How to create PowerShell Azure Function? You can able to create a PowerShell Azure Function using the Visual Studio Code IDE.

  • Create a PowerShell Azure Function Project using Visual Studio Code IDE.
  • While creating the Project, Choose the Project location and PowerShell as the development language.
  • Choose the trigger that will invoke your PowerShell Azure Function.
  • Provide a name for your PowerShell Azure Function and choose the Authorization level based on your requirement.
  • Select the the region for your resource.

Subscribe to Our YouTube Channel for more videos

How To Create PowerShell Azure Function

Well, here we will discuss How to create PowerShell Azure Function. Before going to the actual development, we will discuss a bit on What is Azure Function? and then we will start creating PowerShell Azure Function.

What Is Azure Function?

Azure Function is an event-based service from the Provider Microsoft that helps us with the existing Azure application platform to execute a few lines of code that helps to trigger some events to perform some operations without getting worried about the application infrastructure. You can find out more information on Azure Function now.

What are the Benefits of Azure Functions using PowerShell

There are couple of benefits that we will get by creating Azure functions using PowerShell. Those are as below.

  1. You will get an excellent support for the hybrid environments.
  2. It’s quite easy to integrate with Azure Portal and Visual Studio Code IDE.

Create PowerShell Azure Function Visual Studio Code

Before starting the actual development activity, we should know the Prerequisites for the developing PowerShell Azure Function.

Prerequisites

  • Make sure to install the Visual Studio Code along with Azure Functions Extensions. If you have not yet installed the Visual Studio Code. Install the Visual Studio Code now.
  • An Azure Free Account or Azure Subscription.

Azure Functions PowerShell Examples

Now that we are ready with all the Prerequisites needed for the development activity, Let’s start the actual development of the Azure Function using PowerShell.

  1. The first step is to open the Visual Studio Code from your local machine and then click on the Azure Button from the left side and the next is click on the Create New Project button in the Functions explorer like below.
Create PowerShell Azure Function Visual Studio Code

2. Now the next step is to choose the location to save your PowerShell Azure Function project, you can click on the Browse button to navigate to the local path in your development machine where you want to save your Azure Function project.

How To Create PowerShell Azure Function Visual Studio Code

3. Now select a language as PowerShell from the below window and this is very important to choose the language as PowerShell.

How To Create PowerShell Azure Function using Visual Studio Code
Azure Functions using PowerShell

4. The next step is to select the trigger as the template. You can choose the trigger as an HTTP trigger that will actually invoke your Azure Function. You can also choose the other type of trigger based on your requirement.

How To Create PowerShell Azure Function in Visual Studio Code
Azure Functions PowerShell examples

5. In the next step, Provide an unique name for your Azure Function and hit Enter.

Create PowerShell Azure Function in Visual Studio Code

6. Now the time to choose the Authorization Level as highlighted below

Create PowerShell Azure Function using Visual Studio Code

7. You can click on the Open in new window option to open the project in a separate window as shown below

Create PowerShell Azure Function Visual Studio Code IDE

8. Now it will start creating the PowerShell Azure Function and once it will create the project, You can able to see the below code is for the run.ps1 file.

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
$name = $Request.Query.Name
if (-not $name) {
    $name = $Request.Body.Name
}

$body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."

if ($name) {
    $body = "Hello, $name. This HTTP triggered function executed successfully."
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

Run PowerShell Azure Function Locally

Now Our Azure Function Project is successfully created. The next step is we need to Test PowerShell Azure Function Locally. So click on the Run button from the left-hand side or you can also press the F5 button to run the PowerShell Azure Function Project in Visual Studio Code IDE.

You can able to see the PowerShell Azure Function Project ran successfully with out any issue and Provided us the below URL

MyNewPowerShellAzureFN: [GET,POST] http://localhost:7071/api/MyNewPowerShellAzureFN

Run PowerShell Azure Function Locally

Now Open your Favorite browser and paste the URL, You can able to see below, it is prompting us to append the name value as a query string parameter along with the function URL

run azure function locally powershell

After appending the name value as the query string parameter, you can able to see we got the expected output as below

run powershell azure function locally visual studio code

Till now, we have created the PowerShell Azure Function in Visual Studio Code and We have verified that it is working fine. Now it is the time to deploy the PowerShell Azure Function to Azure.

Deploy PowerShell Azure Function From Visual Studio Code

Like other Azure Functions, We can also deploy PowerShell Azure Function From Visual Studio Code with few easy steps. Follow the below steps to Publish PowerShell Azure Function From Visual Studio Code.

The first step is, click on the Azure button again and then click on the Deploy to Function App button as highlighted below.

Deploy PowerShell Azure Function From Visual Studio Code

Now, the next step is to click on the + Create new Function App in Azure button.

Deploy PowerShell Azure Function Visual Studio Code

Now on the below window, provide an unique name for the function app and hit the Enter.

Publish PowerShell Azure Function Visual Studio Code
deploy azure function powershell

Select the runtime stack version from the below window. Select the PowerShell Core 7.0 as the runtime stack. An important point to note down here is Do not select the PowerShell Core 6.2 version here as it is already deprecated.

Publish PowerShell Azure Function From Visual Studio Code
Azure Functions PowerShell Core

The next is, you need to select the location for the new Azure resource and Now it will start creating all the Azure components needed for the deployment and it will take few seconds and it will show you deploying the PowerShell  Azure Function to the Azure Portal.

How to publish Python Azure Function To Azure from Visual Studio Code

Now if you will see the output window it will start creating the Azure component one by one then after a few minutes, it will deploy your PowerShell Azure Function To Azure Successfully.

Publish PowerShell Azure Function using Visual Studio Code

So this the way for Creating PowerShell Azure Function using VS Code and For Deploying PowerShell Azure Function From VS Code.

How To Create A PowerShell Azure Function Using The Azure Portal

Well, here we will discuss How To Create A PowerShell Azure Function Using The Azure Portal. Follow the below steps for Creating PowerShell Azure Function In Azure Portal.

Azure HTTP PowerShell Function App

  1. Log in to the Azure Portal (https://portal.azure.com/)
  2. Search for the Function App in the Azure Portal and click on the search result
Create A PowerShell Azure Function Using The Azure Portal

3. On the Azure Function App page, Click on the + Add button to create a new Azure Function App.

Then, Provide the below Details on the Create Function App Page

  • SubscriptionYou need to Choose the Correct Azure Subscription which you want to use to create the PowerShell Azure Function.
  • Resource Group Either you can choose your existing Resource Group or if you don’t have an existing Resource group, click on the Create new link to create a new Resource Group.
  • Function App name: Provide a unique name for your Azure Function App.
  • Publish: You need to Choose the Code option here.
  • Runtime Stack: Make sure to choose the PowerShell Core as the Runtime Stack.
  • Version: Choose the latest version of PowerShell Core here.
  • Region: Select the Region for your Azure Resource.

Keep the other tab details as it is, Now after you provide all the above details, then click on the Review + create button.

Create A PowerShell Azure Function Using The Azure Portal
Create a PowerShell Function App in the Azure Portal

One more thing to note down here is, Azure Function using the PowerShell Core is now only supported for the Windows Operating System. If you will navigate to the Hosting tab, you can able to see that the Linux option is disabled.

Develop A PowerShell Azure Function Using The Azure Portal
Create PowerShell Azure Function

4. After you clicked on the Review + create button, It will validate all the details provided by you. If all the option values provided by you are correct then it will enable the Create button and you can able to see the Create button on the next window. Now click on the Create button to create the Azure Function App.

5. It will take a few seconds to create the Azure Function App, Now you can able to see below the deployment is completed now. Click on the Go to resource button to navigate to the Azure Function App that you have created just now.

How To Develop A PowerShell Azure Function Using The Azure Portal
How to create a PowerShell Azure Function using the Azure Portal

Creating PowerShell Azure Function

Now the next step is to create the Azure Function since our PowerShell Azure Function App is ready now. Click on the Functions option from the left navigation and then click on the + Add button on the Function App page as highlighted below.

How To write A PowerShell Azure Function Using The Azure Portal

Next, On the Add Function page, Select the Development environment as Develop in portal and Select the HTTP trigger as the Azure Function template and then Provide a name for the Azure Function and select the Authorization Level as Function and then finally, click on the Add button to create the Powershell Azure Function as shown below.

Write A PowerShell Azure Function Using The Azure Portal
create azure function

Now the PowerShell Azure Function is created Successfully. Click on the Code + Test option from the left navigation as highlighted below to see the code for the Run.ps1 file.

Build A PowerShell Azure Function Using The Azure Portal
How to Create and edit PowerShell Function code

Below is the complete code for the run.PS1 file. If you want to modify the code based on your requirement, You can modify the code in the run.PS1 file and then click on the Save button to save the modified code.

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
$name = $Request.Query.Name
if (-not $name) {
    $name = $Request.Body.Name
}

$body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."

if ($name) {
    $body = "Hello, $name. This HTTP triggered function executed successfully."
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

Now to test the Azure Function if it is working fine, click on the Test/Run button and then select the HTTP method and Provide the Body in the JSON format in the Input Window and then click on the Run button.

Create A PowerShell Azure Function Azure Portal

Now if you will see below in the Output window, we got the HTTP response code as 200 OK and we also got the proper HTTP response content.

Develop A PowerShell Azure Function Azure Portal

So. this is how to develop a PowerShell Azure Function in Azure Portal.

Azure Function Run PowerShell Script

Well, here we will discuss How to Run a PowerShell Script from an Azure Function. Sometimes, you will get to run one PowerShell script to delete some Azure resources daily or to perform some clean-up activity daily via a PowerShell Script. In this type of Scenario, Azure Function is one of the best solutions.

For the above requirement, you can create a PowerShell Timmer Trigger Azure Function and you can add your PowerShell Script in the run.PS1 file and then schedule it to run in a particular time interval based on your business requirement. This solution will work like charm for the above requirements.

Create A PowerShell Timmer Trigger Azure Function Azure Portal

So since we have already created A PowerShell Azure Function App above i.e NewPowerShellAzureFNApp , I am going to use the same Function App to create the PowerShell Timmer Trigger Azure Function. If you want you can create a new PowerShell Azure Function App and then you can create the PoweShell Timmer Trigger Azure Function in that.

So let’s create the Timmer Trigger Azure Function using the following steps

Click on the Functions option from the left navigation and then click on the + Add button on the Function App page as highlighted below.

How To Create A PowerShell Timmer Trigger Azure Function Azure Portal

On the Add function window, select the Development environment option as Develop in portal and then select the Timer Trigger as the template.

Create A PowerShell Timmer Trigger Azure Function using Azure Portal

Then Provide a name for your Timmer Azure Function and provide a CORN expression for the schedule option on which the Azure function is going to execute. Here we have provided the CORN expression here as 0 * /5 ****. This means that the Azure Function is going to execute on each 5 minutes duration. Simillarly if We will mention like 0 * /2 **** means the Azure Function will execute on each 2 minutes interval of time. You can find more details on CORN expressions now.

Azure Function Run PowerShell Script

After you fill all the details above click on the Add button to create the PowerShell Timmer Azure Function. You can able to see now the Timmer trigger Azure Function is created successfully. Click on the Code + Test link from the left navigation, you can able to see the Powershell Code for the run.ps1 file.

Develop A PowerShell Timmer Trigger Azure Function Azure Portal

Below is the complete code that is present in the run.ps1 file

# Input bindings are passed in via param block.
param($Timer)

# Get the current universal time in the default string format.
$currentUTCtime = (Get-Date).ToUniversalTime()

# The 'IsPastDue' property is 'true' when the current function invocation is later than scheduled.
if ($Timer.IsPastDue) {
    Write-Host "PowerShell timer is running late!"
}

# Write an information log with the current time.
Write-Host "PowerShell timer trigger function ran! TIME: $currentUTCtime"

Now you can add the PowerShell script in this run.PS1 file based on your requirement and then click on the save button. Then it will execute the Azure function on the duration that you mentioned as the CORN expression based on your business need. One more thing, you can always click on the Test/Run button to test if the Timmer trigger Azure Function is working fine or not.

This is How you can Develop a PowerShell Timmer Trigger Azure Function and run a PowerShell script from the Azure Function.

Azure Function PowerShell Queue Trigger

Since we have already created the Azure Function App above, Here I will use the same Azure Function app.

If you want to create a separate Azure Function App, then Follow steps 1 to 5 from the How To Create A PowerShell Azure Function Using The Azure Portal section to create the Azure Function App.

Now let’s the PowerShell Queue trigger Azure Function.

Create A PowerShell Queue Trigger Azure Function Azure Portal

  • Click on the Functions option from the left navigation and then click on the + Add button on the Function App page as highlighted below.
How To Create A  PowerShell Queue Trigger Azure Function Azure Portal
  • On the Add function window, select the Development environment option as Develop in portal and then select the Azure Queue Storage Trigger as the template. Then click on the Add button.
Create A PowerShell Queue Trigger Azure Function Azure Portal
  • You can able to see now the PowerShell Queue Trigger Azure Function is created successfully. Click on the Code + Test link from the left navigation, you can able to see the Powershell Code for the run.ps1 file.
Azure Function PowerShell Queue Trigger

Below is the complete code for the run.ps1 file.

# Input bindings are passed in via param block.
param([string] $QueueItem, $TriggerMetadata)

# Write out the queue message and insertion time to the information log.
Write-Host "PowerShell queue trigger function processed work item: $QueueItem"
Write-Host "Queue item insertion time: $($TriggerMetadata.InsertionTime)"

Modify the above code based on your requirement and then save and finally click on the Test/Run button to get the desired output.

This is how you can develop a PowerShell Queue Trigger Azure Function in Azure Portal.

Azure Functions Powershell Modules

You can check out a complete article on What Modules Are Available In Azure Functions PowerShell now.

You may also like Following the below Articles

Wrapping Up

Well, In this article we discussed What are the Benefits of Azure Functions using PowerShell, How To Create PowerShell Azure Function, Azure Functions PowerShell Examples, Create PowerShell Azure Function Visual Studio Code, Run PowerShell Azure Function Locally, Deploy PowerShell Azure Function From Visual Studio Code and we also discussed How To Create A PowerShell Azure Function Using The Azure Portal, Azure HTTP PowerShell Function App, Azure Function Run PowerShell Script and Create A PowerShell Timmer Trigger Azure Function Azure Portal, Azure Function PowerShell Queue Trigger. Hope you have enjoyed this article !!!