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

Previously I had blogged about creating a PowerShell functions using v1 of Azure Functions but in recent updates; PowerShell Core 6 (Preview) is now supported in v2.

  • Want to run small snippets of code in the cloud?
  • Without the need to deploy adequate infrastructure to do so?

Azure Functions allow you to do so, with current supported languages in v2

  • C# (.NET Core 2.2)
  • JavaScript (Node 8 & 10)
  • F# (.NET Core 2.2)
  • Java (Java 8)
  • PowerShell (Core 6 Preview)
  • Python (3.6 Preview)

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

Lets create the Azure Function App

$FunctionName = "tamopsfunctestv2"
$ResourceGroup = "functv2"
$region = "eastus2"
$StorageAccount = "tamopsfuncsatest"

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" --runtime powershell

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 will be creating a function called Function1 , the app will run a basic Write-Output

A folder has been created: tamopsfunctestv2 with two files:-

  • function.json – will holder the timerTrigger to run the function at 10am every day
  • run.ps1 – will contain the basic Write-Output script

function.json

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

run.ps1

param($Timer)
Write-Output "Function1 run PowerShellScript";

Now zip the folder and upload .zip to Azure Function App

#Zip the folder to upload the Azure Function
Compress-Archive -Path * -DestinationPath Function1.zip

$FunctionName = "tamopsfunctestv2"
$ResourceGroup = "functv2"

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

Once successfully uploaded, Function1 will now be displayed within the Function App tamopsfunctestv2

A manual run of the Function to confirm

You now have successfully uploaded an ran an Azure PowerShell Function v2.

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 )

Google photo

You are commenting using your Google 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