Azure PowerShell 函数在 VS Code 中本地运行成功。但是,当部署到函数并在 Azure 门户中运行时,它会失败并出现以下错误:
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' ...]
# 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
已验证在requirements.psd1中添加的模块在PowerShell库中可用。我的理解是,如果 PowerShell 库中提供了模块,则应自动下载这些模块。但是,它在门户中失败。尝试手动将模块复制到站点 wwwroot\Modules 文件夹中,但仍然没有帮助。对于缺少什么以及如何修复它已经没有想法了。任何帮助将不胜感激!谢谢你。
感谢@Ian-kemp 的评论。
如果您需要的模块在 PowerShell 库中可用,您可以使用托管依赖项在 PowerShell 函数中使用它们。
host.json
中启用 托管依赖项功能,并在
requirements.psd1
中提及您的模块。Azure Functions 将确保这些模块已安装并可用于函数。
Powershell
:检查可用的
Get-Module -ListAvailable
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
个人资料.ps1:
if ($env:MSI_SECRET) {
Disable-AzContextAutosave -Scope Process | Out-Null
Connect-AzAccount -Identity
}
要求.psd1:
@{
'Az'='2.*'
'SqlServer' = '22.*'
}
主机.json:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"managedDependency": {
"enabled": true
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[4.*, 5.0.0)"
}
}
门户回复:
参考资料: