Azure PowerShell function works locally in VS Code successfully. However, when deployed to a function and run in Azure portal, it fails with the following error:
2023-12-14T21:05:44Z [Verbose] Sending invocation id: '3e85a40a-a930-78d8-be83-f53bd9847ab1
2023-12-14T21:05:44Z [Verbose] Posting invocation id:3e85a40a-a930-78d8-be83-f53bd9847ab1 on workerId:e8588e50-b2ad-4012-bf5b-92df7aa00739
2023-12-14T21:05:44Z [Warning] The Function app may be missing the 'Az.Accounts' module. If 'Az.Accounts' is available on the PowerShell Gallery, add a reference to this module to requirements.psd1. Make sure this module is compatible with PowerShell 7. For more details, see [here](https://aka.ms/functions-powershell-managed-dependency).
2023-12-14T21:05:44Z [Error] ERROR: The specified module 'Az.Accounts' was not loaded because no valid module file was found in any module directory.
Exception :
Type : System.IO.FileNotFoundException
Message : The specified module 'Az.Accounts' was not loaded because no valid module file was found in any module directory.
HResult : -2147024894
TargetObject : Az.Accounts
CategoryInfo : ResourceUnavailable: (Az.Accounts:String) [Import-Module], FileNotFoundException
FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
InvocationInfo :
MyCommand : Import-Module
ScriptLineNumber : 9
OffsetInLine : 1
HistoryId : 1
ScriptName : C:\home\site\wwwroot\Func_Sample_1\run.ps1
Line : Import-Module Az.Accounts -Force
PositionMessage : At C:\home\site\wwwroot\Func_Sample_1\run.ps1:9 char:1
+ Import-Module Az.Accounts -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot : C:\home\site\wwwroot\Func_Sample_1
PSCommandPath : C:\home\site\wwwroot\Func_Sample_1\run.ps1
InvocationName : Import-Module
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, C:\home\site\wwwroot\Func_Sample_1\run.ps1: line 9
PipelineIterationInfo :
[... Repeat for 'Az.Resources' and 'SqlServer' ...]
requirements.psd1:
# See [here](https://aka.ms/functionsmanageddependency) for additional information.
@{
'Az.Accounts' = '2.*'
'Az.Resources' = '6.*'
'SqlServer' = '22.*'
}
# run.ps1:
using namespace System.Data.SqlClient
param($Request, $TriggerMetadata)
# Import the necessary modules
Import-Module Az.Accounts -Force
Import-Module Az.Resources -Force
Import-Module SqlServer -Force
# Authenticate to Azure
Connect-AzAccount
Write-Host "Request database access token for managed identity"
$MI_Token = (Get-AzAccessToken -ResourceUrl https://database.windows.net ).Token
Verified that the modules added in requirements.psd1 are available in the PowerShell Gallery. My understanding is that modules should be downloaded automatically if they are available in the PowerShell Gallery. However, it fails in the portal. Tried manually copying the modules into the site wwwroot\Modules folder, but it still didn't help. Running out of ideas on what's missing and how to fix it. Any help would be greatly appreciated! Thank you.
