How to run Powershell in Azure Functions

If you have been following me, you know that I am a avid blogger on Dynamics 365 and Power Platform related stuffs. However in today’s world every technology is connected and while I am a MVP in Business solutions, more often than not I tend I have get dirty with Azure stuffs.

And here is one such requirement. I need to execute PowerShell script through an Azure function. To be honest, I haven’t done this earlier. I have written lots of PowerShell cmdlets but executing them in Azure function? I haven’t tried them yet.

So let’s get started here. You shall need an Azure subscription to do this. In my demo, I will get the timezone of a city using the Get-PnPTimeZoneId cmdlet.

The first step is to create a function app. Navigate to Azure Portal and create a function app.

PowerShell from Azure functions.

Enter details relevant to you. What is important here are the fields “Runtime stack” and Version. Runtime stack I have chosen as PowerShell Core.

PowerShell from Azure functions.

We have completed the main step. Wait for the function deployment to complete.

Once the function app deployment is successful, navigate to Functions and create a function of type HttpTrigger.

PowerShell from Azure functions.

PowerShell from Azure functions.

In the “Code + Test” tab, use the below 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."

if (-not (Get-Module -listavailable -Name "SharePointPnPPowerShellOnline")) {
    Install-Module -Name SharePointPnPPowerShellOnline -Scope CurrentUser -force
}


# Interact with query parameters or the body of the request.
$city = $Request.Query.City

$timezone = "";

if ($city) {
    [Console]::WriteLine("The name of the city is " + $city)
    $timezone = Get-PnPTimeZoneId -Match $city
}

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

Observe the code highlighted in bold. The Powershell shifts with basic cmdlets in Azure function and more often than not, you need to install additional modules.

In my case the cmdlet Get-PnPTimeZoneId is in module SharePointPnPPowerShellOnline. Hence first I am checking if it is installed, if not I am installing it.

The remaining part of the code is self explanatory. When I run it with the appropriate query string, it return the following output.

PowerShell from Azure functions.

Hope this helped!

You might also like the below posts.

Debajit Dutta
Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

2 thoughts on “How to run Powershell in Azure Functions”

  1. I’m recommending to us the PnP.PowerShell is the current version of the module. The old version is now retired and archived, but it works for sure.

    Also, you don’t have to manually check and install the module (PnP or any other) in your code. A better way is to use dependency management (https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-powershell?tabs=portal#dependency-management).

    You can add something like ‘PnP.PowerShell’ = ‘1.*’ to the requirements.psd1 file of the function. This way you will always have the latest version available.

    You can use the App files service of the Function app to access this file.

    1. Debajit Dutta (Business Solutions MVP)

      thanks MikkoK, appreciate this input. Something new to learn for me. Let me try this out as well.

      And again thanks for the information.

      -Debajit

Comments are closed.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading