Rod*_*ble 13 powershell jenkins jenkins-plugins jenkins-pipeline
是否可以直接在管道groovy脚本中调用PowerShell命令?在Jenkins中使用自定义作业时,我可以使用PowerShell插件调用该命令.但是在groovy脚本中没有使用它的代码片段.
我也试过,sh()但似乎这个命令不允许命令中有多行和注释.
elo*_*lou 23
从Groovy-Script调用PowerShell脚本:
bat命令.errorlevel正确返回Error Code()变量(EXIT 1应该导致FAILED作业).$LastExitCode将被考虑.对于那个porpuse我写了一个groovy方法,可以集成在任何管道脚本中:
def PowerShell(psCmd) {
psCmd=psCmd.replaceAll("%", "%%")
bat "powershell.exe -NonInteractive -ExecutionPolicy Bypass -Command \"\$ErrorActionPreference='Stop';[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;$psCmd;EXIT \$global:LastExitCode\""
}
Run Code Online (Sandbox Code Playgroud)
[编辑]我添加了UTF8 OutputEncoding:适用于Server 2016和Win10.[/ EDIT] [编辑]我添加了'%'掩码[/编辑]
在您的Pipeline-Script中,您可以像这样调用您的脚本:
stage ('Call Powershell Script')
{
node ('MyWindowsSlave') {
PowerShell(". '.\\disk-usage.ps1'")
}
}
Run Code Online (Sandbox Code Playgroud)
使用该方法的最好的方法是,您可以CmdLet在脚本中调用而不必执行此操作,这是最好的实践.
调用ps1来定义CmdLet,然后调用CmdLet
PowerShell(". '.\\disk-usage.ps1'; du -Verbose")
Run Code Online (Sandbox Code Playgroud)
.当脚本返回错误代码(应该是首选)时,推迟脚本以确保您的步骤失败,&如果您不关心它,请使用.| 归档时间: |
|
| 查看次数: |
27676 次 |
| 最近记录: |