Azure Function PowerShell

How To Create PowerShell Azure Function

In this Azure tutorial, we will discuss Azure Functions PowerShell and how to create PowerShell Azure Function.

Subscribe to Our YouTube Channel for more videos

Azure Function PowerShell

Well, here we will discuss How to create a PowerShell Azure Function. Before going to the actual development, You can check out What Is Azure Functions, and then we will discuss the benefits of PowerShell Azure Function. Finally, we will start creating PowerShell Azure Function.

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

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. The next is to click on the Create New Project button in the Functions Explorer like below.
azure function powershell

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

azure function powershell example

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

azure function app powershell example

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

azure function app powershell

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

Azure Functions PowerShell examples

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

azure functions powershell

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

create azure function powershell

8. Now, it will start creating the PowerShell Azure Function, and once it creates the project, You can 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 to Test the PowerShell Azure Function Locally. 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 see the PowerShell Azure Function Project ran successfully without any issues and provided us with the URL below

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

azure function run powershell script

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

azure powershell function

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

powershell function app

Now, we have created the PowerShell Azure Function in Visual Studio Code, and We have verified that it is working fine. Now, it is 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 a few easy steps. Follow the below steps to Publish PowerShell Azure Function From Visual Studio Code.

The first step is to 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.

powershell azure function example

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

Publish PowerShell Azure Function Visual Studio Code

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

Azure Functions PowerShell Core

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

Create a PowerShell Function App in the Azure Portal

Now, if you see the output window, it will start creating the Azure components individually. Then, after a few minutes, it will deploy your PowerShell Azure Function To Azure Successfully.

function app powershell

So this is the way to create a PowerShell Azure Function using VS Code and Deploying a PowerShell Azure Function From VS Code.

How To Create A PowerShell Azure Function Using The Azure Portal

Follow the below steps for Creating PowerShell Azure Function In Azure Portal.

  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
powershell azure functions

3. On the Azure Function App page, Click 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 that 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: 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, click the Review + Create button.

Azure HTTP PowerShell Function App

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

Create PowerShell Azure Function

4. After you click 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 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 you created just now.

How To Develop 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 Create and edit PowerShell Function code

Next, On the Add Function page, Select the Development environment as Develop in portal, Select the HTTP trigger as the Azure Function template, 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.

How to create a PowerShell Azure Function using the Azure Portal

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

Below is the complete code for the run.PS1 file. If you want to modify the code based on your requirements, 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 to see if it works fine, click on the Test/Run button, select the HTTP method, 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, as 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

FAQs

What are the Benefits of Azure Functions using PowerShell

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

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

You may also like Following the below Articles

Wrapping Up

Well, In this article, we discussed How To Create PowerShell Azure Function, and Azure Functions PowerShell Examples. I hope you have enjoyed this article !!!