Using PowerShell Modules in Azure Functions

Steve

February 24th, 2017

Previously, I blogged about how I created PowerShell GitHub Dashboard using Azure Functions to run a PowerShell script and didn’t use PowerShell Modules as I didn’t find an easy way to do it with Azure Functions. Stefan informed me that you can easily do it using FTP! Today, I’m publishing a guest blog post that Stefan authored that walks you through how to use PowerShell Modules in Azure Functions!

Steve Lee published a couple of blog posts about how he created a PowerShell Open Source Community Dashboard in PowerBi. In his last blog post he explained how he used PowerShell, Azure StorageTable, Azure Function and PowerBi to create the Dashboard.

In his solution, the Azure Function is executing a PowerShell script which calls the Github REST APIs and stores the result in an Azure StorageTable, finally queried by PowerBI.

Azure Functions and especially PowerShell Azure Functions are something I’m interested in for the last couple of weeks. I already wrote a blog post called “PowerShell Azure Functions lesson learned“. If you are just starting to explore PowerShell Azure Functions I would highly recommend to look.

In his blog post Steve mentions that within Azure Functions you only can run a PowerShell script and that you cannot use PowerShell modules within your PowerShell Azure Function. Well that’s not correct, there is a way to use PowerShell Modules within your PowerShell Azure Function.

In this blog post I’ll explain how you could start using PowerShell Modules in Azure Functions. I’ve to give credit to David O’Brien for introducing PowerShell Azure Functions to me.

Which PowerShell Modules are available with a PowerShell Azure Function?

First you need to create an Azure Function. On the Github page about Azure Functions you can find all the info to get started.

After creating your PowerShell Azure Function, you can start exploring the current available PowerShell Modules just like you would do on your local machine.

Get-Module -ListAvailable | Select-Object Name, Version | Sort-Object -Property Name

Let first start with retrieving the currently installed PowerShell Modules in Azure Functions by outputting the result to the log window in Azure Functions. We are also interested in the locations of the PowerShell Modules.

Write-Output ‘Getting PowerShell Module’ $result = Get-Module -ListAvailable | Select-Object Name, Version, ModuleBase | Sort-Object -Property Name | Format-Table -wrap | Out-String Write-output `n$result

Copy above PowerShell code into the run.ps1 Azure Function and click on Run.

If we expand the log window we see that the following PowerShell Modules are installed.

I prefer to see the output result in my PowerShell ISE so I often use the following code in my Azure Function.

$result = Get-Module -ListAvailable | Select-Object Name, Version, ModuleBase | Sort-Object -Property Name | Convertto-Json -depth 1 Out-File -encoding Ascii -FilePath $res -inputObject $Result

You can now call your Azure Function from the PowerShell ISE with the following PowerShell script.

$HttpRequestUrl = ‘[enter azure function url here]’ Invoke-RestMethod -Uri $HttpRequestUrl -Method POST -ContentType ‘application/json’ | ConvertFrom-Json

This results in the following.

The next step is checking the Environment variable PSModulePath to find out where all the PowerShell Modules are stored.

$result = $env:psmodulepath | Convertto-Json -depth 1 Out-File -encoding Ascii -FilePath $res -inputObject $Result

Where can we store our own PowerShell Modules?

Well because Function apps are built on App Service, all the deployment options available to standard web apps are also available for function apps 🙂 The App Service has a deployment option to configure deployment credentials.

Remark: When you configure a FTP/Deployment username and password this applies to all your Web Apps within your Microsoft Azure Account.

To upload PowerShell Modules via FTP we need to configure the Function App Settings.

Select Function app settings -> Go to App Service Settings to configure the Deployment credentials.

Next select in the App Service settings for the Azure Function Deployment Credentials.

Enter a FTP/deployment username and password.

Now you are ready to connect via a FTP client to your Azure Function (Web App).

You can find the FTP Hostname in your Azure Portal.

Connect via FTP Client to Azure Function Web App

Open your favorite FTP Client and connect to the FTP endpoint. I’m using Filezilla as my favorite FTP client.

Copy and past host, username from App Service overview page on the Azure Portal and click on Quickconnect.

After connecting via FTP we can create a new Module folder under wwwroot\HttpTriggerPowerShellDemo (or your name of your Azure Function). Create a new directory called Modules.

After the creation of the Modules directory we can upload our PowerShell Module(s) to this remote directory. Make sure you have first downloaded your module to your local system. We are going to upload my favorite PowerShell Module Wunderlist.

Drag and drop the local Wunderlist module to the remote Modules folder.

You have now uploaded the Wunderlist module to your Azure Function.

How can we start using this module in our Azure Function?

First we can check if we can load the module and return the help from one of the Functions in the Module.

Use the following code in your Azure Function:

Write-Output “Loading Wunderlist Module” import-module ‘D:\Home\site\wwwroot\HttpTriggerPowerShellDemo\Modules\Wunderlist\1.0.10\Wunderlist.psm1’ $Result = Get-Help Get-WunderlistUser | Out-String Write-Output $Result

Result:

To get my Wunderlist Module working you need to also configure a ClientId and AccessToken. For more information you can check the following blog post Using Wunderlist Module in Azure Automation–Part 2. Long story short, you need to store the ClientID and AccessToken as Environment variables using the Web App Settings.

Go to Function App Settings and click on Configure app settings.

Add ClientID and AccessToken variables in the Application settings. Don’t forget to Save the new settings.

We can now use these Environment variables in our Azure Function without any extra configuration needed.

As a final test, we are going to test if we can create a new Wunderlist Task from our Azure Function.

By using a HTTP Post method with a body value as input for our Function we can do some more advanced Azure Function activities.

Use the following code in your Azure Function:

# Get the input request $in = Get-Content $req -Raw | ConvertFrom-Json Write-Output $in.Title # Import Wunderlist Module Write-Output “Loading Wunderlist Module” import-module ‘D:\Home\site\wwwroot\HttpTriggerPowerShellDemo\Modules\Wunderlist\1.0.10\Wunderlist.psm1’ # Create new Wunderlist Task Write-Output “Creating Wunderlist Task” $Result = New-WunderlistTask -listid ‘267570849’ -title $in.Title Write $Result

Hope you have now learned how to use your “own” PowerShell modules within your PowerShell Azure Functions.

Stefan Stranger Secure Infrastructure Consultant Microsoft