10 Upgrading your Azure Function Apps to run on PowerShell 7.4
andystaples редактировал(а) эту страницу 2024-10-08 15:06:41 -06:00

Note: Support for PowerShell 7.2 in Azure Functions will end on November 8, 2024. While your existing PowerShell Function Apps running on 7.2 will continue to run, we'll no longer provide updates or customer service for PowerShell 7.2. To ensure that your Functions apps are fully supported, you'll need to upgrade them to PowerShell 7.4. Learn more about PowerShell End-of-support dates.

Follow this guide to upgrade your existing Azure PowerShell Function Apps to PowerShell 7.4

Before you begin

Before your upgrade your app to PowerShell 7.4, please review the breaking changes below and plan to test your app in a non-production environment.

Breaking changes

OrderedHashtable instead of Hashtable in trigger and input bindings

When you use PowerShell 7.4 in Azure Functions, trigger and input binding data passed to your function is now an object of type System.Management.Automation.OrderedHashtable instead of a System.Collections.Hashtable, and the order of properties corresponds to the order in which they appear in the original data source. This also means that the property names are now case-sensitive.

For example, your HTTP-triggered function may receive the following JSON payload:

{
    "name": "John"
}

When using PowerShell 7.2 and earlier, you could access the value of the name property by using either $Request.Body.name or $Request.Body.Name. In PowerShell 7.4, you must match the case of the property name in the JSON payload. In this example, you must use $Request.Body.name to access the value of the name property. The $Request.Body.Name expression will return $null without reporting any error.

If you cannot guarantee the case of the property names in the JSON payload because your client can use different casing and you are not in control of the client, you can convert the OrderedHashtable to a case-insensitive Hashtable using the following code:

$convertedBody = [HashTable]::New($Request.Body, [StringComparer]::OrdinalIgnoreCase)

If your data may contain nested OrderedHashtable objects, you can also convert all OrderedHashtable objects to Hashtable objects using the following code:

function ConvertTo-Hashtable ([System.Management.Automation.OrderedHashtable] $OrderedHashtable) {
    $hashtable = @{}
    foreach ($entry in $OrderedHashtable.GetEnumerator()) {
        $value = $entry.Value
        $hashtable[$entry.Key] =
            if ($value -is [System.Management.Automation.OrderedHashtable]) {
                ConvertTo-Hashtable $value
            } else {
                $value
            }
    }
    return $hashtable
}

$convertedBody = ConvertTo-Hashtable $Request.Body

Using either of these methods, $convertedBody will contain a copy of the $request.Body with a case-insensitive hash table. After this conversion, the order of properties will be lost, but you can access the values using case-insensitive property names, as in PowerShell 7.2 and earlier.

This change corresponds to the following change in PowerShell 7.3: Change ConvertFrom-Json -AsHashtable to use ordered hashtable #17405.

Invoke-WebRequest/Invoke-RestMethod changes

Beginning in PowerShell 7.4, character encoding for requests defaults to UTF-8 instead of ASCII. This has caused issues when migrating apps which rely on specific encoding settings from the 7.2 implementation or which may encounter non-ANSI characters in request content.

Other potentially breaking changes to these cmdlets can be found throughout the Invoke-WebRequest documentation and the Invoke-RestMethod documentation

PowerShell 7.3 and PowerShell 7.4 breaking changes

Please review the following notes on breaking changes in PowerShell 7.3 and 7.4:

How to upgrade

To run your Function App on PowerShell 7.4, ensure the value of FUNCTIONS_EXTENSION_VERSION is set to ~4.

In PowerShell Functions, the value "~7" for FUNCTIONS_WORKER_RUNTIME_VERSION refers to "7.0.x". We do not automatically upgrade PowerShell Function apps that have "~7" to "7.4". Going forward we will require that apps specify both the major and minor version they want to target. Hence, it is necessary to mention "7.4" if you want to target "7.4.x"

Developing PowerShell 7.4 function apps locally using VS Code

  1. Open your Azure Functions App using VS Code
  2. Navigate to "local.setting.json"
  3. Add the property "FUNCTIONS_WORKER_RUNTIME_VERSION" and set the value to "7.4".

The local.settings.json file should look like:

{ 
 "IsEncrypted": false, 
 "Values": { 
   "AzureWebJobsStorage": "", 
   "FUNCTIONS_WORKER_RUNTIME": "powershell", 
   "FUNCTIONS_WORKER_RUNTIME_VERSION" : "7.4" 
 } 
} 

Upgrading your Function App to run on PowerShell 7.4

Using Azure Portal

Note: Upgrading the language version of Linux Function Apps is currently not supported via the portal experience. For Linux, follow the instructions below for PowerShell/ARM

  1. Login to Azure Portal
  2. Navigate To Azure Functions Portal
  3. On the left Panel, click on Configuration under Settings
  4. Click on Function Runtime Settings blade, verify that the Runtime version is set to ~4. If not, set it to ~4 and click Save

Functions Runtime Settings


  1. Click on General settings blade, change the PowerShell Version Setting to 7.4. Click Save PowerShell Version

Using Azure PowerShell

For Windows

  1. To verify that your function app is running on Azure Functions Runtime v4, execute the following commands:
$SubId =  
$ResourceGroupName = ... 
$AppName = ... 

Get-AzFunctionAppSetting  -Name $AppName -ResourceGroupName $ResourceGroupName -SubscriptionId $SubId  
  1. If the value of the AppSetting FUNCTIONS_EXTENSION_VERSION is not set to ~4, that means you are not running on the latest Azure Functions v4 runtime. Execute the following command to upgrade:
Set-AzResource -ResourceId "/subscriptions/$SubId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$AppName/config/appsettings" -UsePatchSemantics -Properties @{  FUNCTIONS_EXTENSION_VERSION  = '~4' } -Force 
  1. When running on Windows, you also need to enable .NET 6.0, which is required by version 4.x of the runtime.
Set-AzResource -ResourceId "/subscriptions/$SubId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$AppName/config/web" -UsePatchSemantics -Properties @{ "NetFrameworkVersion"  = 'v6.0' } -Force
  1. Once the Azure Function runtime is upgraded to v4, execute the command below to migrate to PowerShell 7.4:
Set-AzResource -ResourceId "/subscriptions/$SubId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$AppName/config/web" -UsePatchSemantics -Properties @{ powerShellVersion = '7.4' } -Force 

For Linux

  1. Since Linux support for Azure Function PowerShell Apps was not available prior to Functions Runtime v4, your App should already be on Functions Runtime v4. To upgrade your Linux PowerShell Function App to 7.4, execute the command below:
Set-AzResource -ResourceId "/subscriptions/$SubId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$AppName/config/web" -UsePatchSemantics -Properties @{ linuxFxVersion = 'PowerShell|7.4' } -Force

Creating Azure Functions using PowerShell 7.4 in ARM template

For Windows

In your ARM template, please specify these settings:

    "siteConfig": { 
        "appSettings": [ 
            ... 
            { 
                "name": "FUNCTIONS_EXTENSION_VERSION", 
                "value": "~4" 
            }, 
            { 
                "name": "FUNCTION_WORKER_RUNTIME", 
                "value": "powershell" 
            }, 
            ... 
        ], 
        ... 
        "powerShellVersion": "7.4", 
        ... 
    } 

For Linux


"siteConfig": {
    "appSettings": [
        ...
        {
            "name": "FUNCTIONS_EXTENSION_VERSION",
            "value": "~4"
        },
        {
            "name": "FUNCTION_WORKER_RUNTIME",
            "value": "powershell"
        },
        ...
    ],
    ...
    "linuxFxVersion": "PowerShell|7.4",
    ...
}