【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name

发布时间 2023-11-02 21:01:15作者: 路边两盏灯

问题描述

Azure Web Job执行Powershell脚本报错 

Select-AzContext : The term 'Select-AzContext' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. 

 

问题解答

根据错误信息,判断是此Web Job执行时,缺失对应的PS模块导致无法正常运行。

针对这种情况,需要通过高级站点(Kudu工具:https://<your app service name>.scm.chinacloudsites.cn/DebugConsole )上传 Az.Accounts 到 \home\site\wwwroot 下

然后,在修改 $Env:PSModulePath 的路径,在其后面添加上 +“;c:\home\site\wwwroot”, 示例如下:

但是,请注意,这样的修改为临时性修改。如果App Service的实例重启后,此处修改会丢失。

所以为了是修改长久有效,可以在Web Job的PowerShell脚本中加入这句话。

$Env:PSModulePath =$Env:PSModulePath + "; c:\home\site\wwwroot"

如图:

 

 

 

[end]