Jenkins:未找到 PowerShell 脚本

问题描述 投票:0回答:1

我有一个由 Jenkins 运行的 Groovy 脚本,该脚本包含指令:

Boolean myValue = powershell(script: "./myScript.ps1", returnStatus: true)

运行 Groovy 脚本时,出现以下错误:

powershell.exe : ./myScript.ps1 : The term './myScript.ps1' is not recognized as the name of a 
            At C:\Program Files (x86)\Jenkins\workspace\MyProject@tmp\durable-f15db675\powershellWrapper.ps1:3 char:1
            + & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
            + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                + CategoryInfo          : NotSpecified: (./myScript.ps1... the name of a :String) [], RemoteException
                + FullyQualifiedErrorId : NativeCommandError
             
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:\Program Files (x86)\Jenkins\workspace\MyProject@tmp\durable-f15db675\powershellScript.ps1:1 char:1
+ ./myScript.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (./myScript.ps1:String) [], ParentContainsErrorRecordEx 
   ception
    + FullyQualifiedErrorId : CommandNotFoundException

myValue
那么
True

Groovy 和 PowerShell 脚本都位于同一目录中(至少在 Jenkins 获取 Groovy 脚本的 Git 存储库上)。

在 Groovy 脚本中,还有其他 PowerShell 调用,但使用直接命令,而不是 ps1 脚本文件。他们都很好。

  1. Jenkins 应该把这些脚本放在哪里?我在工作区找不到它们。
  2. 问题是否是由 PowerShell 脚本的权限引起的?
  3. 为什么 Groovy 脚本不会失败?为什么还要继续?
  4. 为什么
    myValue == true
    ?是因为
    returnStatus
    的说法吗?

编辑:为脚本指定的路径是错误的。这 ”。”目录指向工作区,而不是 Groovy 脚本所在的目录。我现在必须找到一种方法来走这条路......

powershell jenkins jenkins-groovy
1个回答
0
投票

如果 PowerShell 脚本与 Groovy 脚本位于同一目录中,则必须通过 后者 的目录路径调用它,而不是 current (

.
):

注:

// Get the full path of the 'myScript.ps1' script located in the same // dir. as the executing Groovy script. powerShellScriptFilePath = new File( new File(getClass().protectionDomain.codeSource.location.path).parent, 'myScript.ps1' ) Boolean myValue = powershell(script: powerShellScriptFilePath, returnStatus: true)
    
© www.soinside.com 2019 - 2024. All rights reserved.