Powershell In Jenkins

Powershell In Jenkins

Using Microsoft PowerShell in Pipeline

  • PowerShell now runs in Non-Interactive mode to prevent interactive prompts from hanging the build
  • PowerShell now runs with ExcecutionPolicy set to “Bypass” to avoid execution policy issues
  • Scripts now exit with $LastExitCode, causing non-zero exit codes to mark a build as failed
  • Added help and list of available environment variables (including English and French translations)

Writing PowerShell code as part of your pipeline is incredibly simple. The step that you will use is simply powershell, and it includes the same optional parameters as the Windows Batch (bat) step, including:

  • returnStdout: Returns the standard output stream with a default encoding of UTF-8 (alternative encoding is optional)

  • returnStatus: Returns the exit status (integer) of the PowerShell script

Which streams get returned when I use returnStdout?
Until the release of PowerShell 5, there were five distinct output streams. PowerShell 5 introduced a sixth stream for pushing “informational” content, with the added benefit of being able to capture messages sent to Write-Host. Each row of the following table describes a PowerShell stream along with the corresponding Cmdlet used for writing to the stream for that particular row. Please keep in mind that stream 6 and associated cmdlets either do not exist or exhibit alternate behavior in versions of PowerShell earlier than version 5.
在这里插入图片描述

If you are using the returnStdout option of the powershell Pipeline
step then only stream 1 will be returned, while streams 2-6 will be
redirected to the console output.

Redirected others stream only keep output stream return when you want to add some debug information in powershell script:
https://stackoverflow.com/questions/30906329/redirect-two-or-more-powershell-streams-other-than-output-stream-to-the-same-fil
在这里插入图片描述

  • The verbose, warning, and debug streams are merged into STDOUT when you run PowerShell scripts via. so you can’t redirect them separately anymore. Only the error stream is different, since it seems to go to both STDOUT and STDERR, where it can be redirected by 1> as well as 2>.
    在这里插入图片描述
  • If you want to redirect the verbose, warning, or debug stream separately you must use -Command instead of -File and do the redirection within PowerShell,
    powershell -Command "C:\test.ps1 3>>powershell.log 4>>powershell.log 5>>powershell.log " , only output stream will be return.
    在这里插入图片描述

So we will use write-debug to log all custom information to
powershell.log file, then use archiveArtifacts ‘powershell.log’
archive the log file in jenkins build job.

What causes a failing exit status?
When you execute a powershell step, it may produce a non-zero exit code and fail your pipeline build. This is very similar to other shell steps with some interesting caveats. Your powershell step may produce a failing exit status in the following instances:

  1. Something in your PowerShell script has thrown an exception

  2. Your PowerShell script explicitly calls exit with a non-zero exit code

  3. Your PowerShell script calls a native application that produces a non-zero $LastExitCode

    $LastExitCode is an automatic variable that is set after executing a native application

  4. Your PowerShell script results in a non-empty error stream (with or without throwing an exception)

https://jenkins.io/blog/2017/07/26/powershell-pipeline/

https://wiki.jenkins.io/display/JENKINS/PowerShell+Plugin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值