Sunday, August 15, 2021

Install AzureAD and AzureADPreview module in PowerShell Function App

One wants to install AzureAD and AzureADPreview in his/her windows PowerShell function app. Modules are already input in requirements.psd1managedDependency is also enabled in host.json. But when executing functions in Azure, error "Could not load file or assembly…" shows up.

 

Analysis:

The problem is rooted in the compatibility issue between PS7(PowerShell Core 7) and module AzureAD and AzureADPreview.

 

As of now, the Functions runtime with PowerShell 7 is being rolled out globally. If one wants to check the powershell Core Version, he/she can simply goes to Function Portal --> Configuration --> General settings.

Linru_Hui_0-1628846374887.png

 

 

AzureAD works out of the box with Powershell 7! People need to import AzureAD with the -UseWindowsPowershell switch.

 

 

Import-Module AzureAD -UseWindowsPowerShell

 

 

 

 

Below is the detailed step one can use in order to install AzureAD and  AzureADPreview module in Azure Function App.

Step 1: In requirements.psd1, input the two modules with their versions, wildcard is recommend to get the latest version of module;

 

 

@{
    # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'.
    # To use the Az module in your function app, please uncomment the line below.
    # 'Az' = '6.*'
    'AzureADPreview' = '2.*'
    'AzureAD' = '2.*'
}

 

 

 

 

Step 2: make sure “managedDependency” is set to true in host.json;

 

 

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  },
  "managedDependency": {
    "enabled": true
  }
}

 

 

 

 

Step 3: import module with below syntax:

 

 

Import-Module AzureAD -UseWindowsPowerShell
Import-Module AzureADPreview -UseWindowsPowerShell

 

 

 

Results:

Modules will be presented under C:\home\data\ManagedDependencies\xxxxxx.r. One can also invoke methods this module provides in his function code.

 

Linru_Hui_0-1628849096770.png

 

 

 

Posted at https://sl.advdat.com/3g8cJPc