如何在TFS 2018中用PowerShell 7执行Azure DevOps自定义任务?
解决TFS 2018自定义任务切换到PowerShell 7执行的问题
TFS 2018的任务执行框架原生不支持将pwsh作为内置执行器节点,直接替换"Powershell"为"pwsh"不会生效,你可以通过以下两种方案解决:
方案一:指定PowerShell 7的可执行文件路径
修改自定义任务的execution配置,保留"Powershell"节点,新增"executable"字段指向PowerShell 7的pwsh.exe路径:
"execution": { "Powershell": { "target": "widgetUpload.ps1", "argumentFormat": "", "workingDirectory": "$(currentDirectory)", "executable": "C:\\Program Files\\PowerShell\\7\\pwsh.exe" } }
如果pwsh.exe已经添加到系统环境变量PATH中,也可以直接写:
"executable": "pwsh.exe"
方案二:通过PowerShell 5.1间接调用PowerShell 7
将任务的目标指向pwsh.exe,并在参数中指定要执行的脚本:
"execution": { "Powershell": { "target": "pwsh.exe", "argumentFormat": "-File \"$(currentDirectory)\\widgetUpload.ps1\"", "workingDirectory": "$(currentDirectory)" } }
这种方式是让原PowerShell 5.1进程启动PowerShell 7的执行器,再运行你的目标脚本。
额外检查项
- 确认构建机器上已安装PowerShell 7,且
pwsh.exe路径正确 - 确保构建代理服务账号有权限访问该路径及目标脚本文件
内容的提问来源于stack exchange,提问作者Fairoz
相关产品推荐
相关产品推荐

