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:
-
Something in your PowerShell script has thrown an exception
-
Your PowerShell script explicitly calls exit with a non-zero exit code
-
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
-
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