Jenkins流水线中的PowerShell步骤失败,原因是“找不到PowerShell”。

3

我有一个运行 v2.107.3 版本的 Jenkins 服务器和一个 Windows 代理节点。一个简单的测试 PowerShell 流水线失败了,因为它找不到 "powershell"。

这是我的测试流水线:

pipeline {
    stages {
        stage('test') {
            steps {
                powershell(script:'Write-Output Hello')
            }
        }
    }
}

代理的响应总是:


C:\Jenkins\workspace\test_ps_remoting@tmp\durable-ccca47a5\powershellWrapper.ps1 : The term 'powershell' 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.
At C:\Jenkins\workspace\test_ps_remoting@tmp\durable-ccca47a5\powershellHelper.ps1:54 char:9
+     & { & $MainScript | Out-FileNoBom -Writer $OutputWriter } *>&1 |  ...
+         ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (powershell:String) [powershellWrapper.ps1], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,powershellWrapper.ps1

我已经成功获取了@tmp中运行的文件的副本,如果我手动执行这些步骤,它似乎完全正常:

. .\powershellHelper.ps1
Execute-AndWriteOutput -MainScript .\powershellWrapper.ps1 -OutputFile out.txt -LogFile log.txt -ResultFile result.txt

创建预期的文件,其“结果”为0,日志为Hello

1
听起来你的环境变量有问题。完整路径 powershell.exe。另外:& { & $ 这是为什么? - Maximilian Burszley
@TheIncorrigible1 这与我无关。这是PowerShell插件放置的内容。我只指定了 powershell('write-host hello')。其余部分是包装器废话。 - Cylindric
@TheIncorrigible1,你关于ENV的提示非常准确。我在管道中添加了PATH = "C:\Windows\System32\WindowsPowerShell\v1.0",似乎已经解决了问题。将其发布为答案,我会接受它。 - Cylindric
他们的样板代码非常奇怪,但无所谓。我很高兴能够帮助。 - Maximilian Burszley
1个回答

2
根据您遇到的错误,该进程很可能找不到PowerShell可执行文件,这意味着:
  1. 路径不完整
  2. 未在PATH环境变量中
如果您修复其中任何一个问题,您的问题应该得到解决。

奇怪的是,Powershell有一个系统级别的PATH条目,但Java进程似乎没有获取它。暂时在我的流程中设置它可以解决问题。 - Cylindric
@Cylindric,看起来它没有读取SYSTEM环境变量,只有USER和/或PROCESS。(三层头痛) - Maximilian Burszley
我不得不取消接受这个答案,因为我意识到在多代理管道中我不能真正设置全局路径,因为我不想干扰其他代理设置,而且它们甚至不都是Windows代理。我还怀疑我永远不会得到一个不是公开的问题的答案。 - Cylindric
2
算了,原来 Jenkins 的另一个管理员已经设置了一个全局环境变量 PATH,将其设置为 /sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin。这真是有趣。 - Cylindric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接