Azure Functions:- Creating a PowerShell Function v1 using Azure CLI

Want to run small snippets of code in the cloud? Without the need to deploy adequate infrastructure to do so? With Azure Functions, this can be achieved!

All you need is to write the code and let Azure worry about the back-end infrastructure to run your code

A range of languages are supported by functions including JavaScript & C#, along with experimental languages including PowerShell – additional supported languages found here

Functions can be ran numerous ways, a couple of common ways to run an Azure function is via a Timer Trigger or HTTP Trigger

  • Timer Trigger:- Function runs at a specific time
  • HTTP Trigger:- Function runs once it has been Trigger by a HTTP request

As PowerShell is an experimental language, it does have some limitations:-

  • Limited to Version 5.1
  • Only runs on Azure Function v1
  • Does not support modules that require writing the registry

Lets create the Azure Function

I have created a ResourceGroup: tamops-funct-test in eastus

$FunctionName = "tamopsfunctest"
$ResourceGroup = "tamops-func-test"
$region = "eastus"
$StorageAccount = "tamopsfuncsa"

az storage account create -n $StorageAccount -g $ResourceGroup -l $region --sku Standard_LRS

az functionapp create -n $FunctionName -g $ResourceGroup --consumption-plan-location $region --storage-account "$StorageAccount"

This will create Azure function: tamopsfunctest  along with its App Service Plan & Storage Account. The App Service Plan created is consumption based

Checking the Azure Function settings, the run-time defaults to v2. this will not allow you to run PowerShell snippets

Running the below will change run-time version to v1

az functionapp config appsettings set -n $FunctionName -g $ResourceGroup  --settings FUNCTIONS_EXTENSION_VERSION=~1

Time to now upload your first PowerShell function, you can have multiple functions under the one function app, the struction is as follows

FunctionAppName
| -Function1
| | -function.json
| | -run.ps1
| -Function2
| | -function.json
| | -run.ps1

For this example, I have created a folder called TestFunction1 with files function.json & run.ps1

run.ps1

Write-Output "TestFunction1 run PowerShellScript";

function.json will holder the timerTrigger to run the function at 10am every day

{
  "bindings": [
    {
      "name": "TestFunctionTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 10 * * *"
    }
  ],
  "disabled": false
}

Zip the folder and upload the the Azure Function

Compress-Archive -Path * -DestinationPath TestFunction1.zip

az functionapp deployment source config-zip -n $FunctionName -g $ResourceGroup --src ./TestFunction1.zip

Function will now be displayed

Clicking run will now run the script run.ps1

8 comments

  1. Pingback: Kainos
  2. Nice concept, but I’ve tried it an an Azure Function running PoSh V1 & V2 and I can’t get past: [Error] az : The term ‘az’ is not recognized as the name of a cmdlet, function, script file, or operable program.

    Any ideas?

    Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s