Azure Functions中PowerShell模块无法从requirements.psd1加载求助
在Azure Functions的PowerShell函数里调用Microsoft Graph的Connect-MgGraph cmdlet时,遇到以下错误:
[Error] ERROR: The term 'Connect-MgGraph' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
已配置requirements.psd1文件如下,且host.json中已开启managedDependency:
@{ # For latest supported version, go to 'https://www.powershellgallery.com/packages/Az'. Uncomment the next line and replace the MAJOR_VERSION, e.g., 'Az' = '5.*' # 'Az' = '10.*' 'Az.Accounts' = '2.5.4' 'Microsoft.Graph.Authentication' = '2.*' }
解决步骤
确认托管依赖配置完整性
检查host.json的managedDependency配置是否符合标准结构,避免拼写或格式错误:{ "version": "2.0", "managedDependency": { "enabled": true } }强制触发依赖同步
托管依赖有时不会自动更新,可通过以下方式强制同步:- 进入Azure门户的函数应用,打开配置 > 应用程序设置
- 添加或更新
FUNCTIONS_POWERSHELL_MANAGED_DEPENDENCIES_AUTOUPDATE设置,值设为true - 重启函数应用;本地开发的话,删除项目根目录的
.powershell文件夹后重新启动项目,让系统重新下载模块。
调整模块版本兼容性
Microsoft.Graph.Authentication2.x和Az.Accounts2.5.4可能存在版本冲突,尝试调整版本:- 给
Microsoft.Graph.Authentication指定具体兼容版本,比如2.16.0 - 或者将
Az.Accounts升级到匹配版本,比如2.13.0
- 给
临时手动导入模块
如果托管依赖仍不生效,可在函数代码开头手动导入模块(注意:会增加启动时间,优先解决托管依赖问题):Import-Module Microsoft.Graph.Authentication -MinimumVersion 2.0验证模块部署状态
登录函数应用的Kudu控制台,进入D:\home\data\ManagedDependencies\202x\xx\xx路径,确认Microsoft.Graph.Authentication文件夹存在且模块文件完整。
内容的提问来源于stack exchange,提问作者MattP

