Getting output values from powershell script from jenkins pipeline Getting output values from powershell script from jenkins pipeline jenkins jenkins

Getting output values from powershell script from jenkins pipeline


Modify your Get-GUID.ps1 script to write output to one file or multiple files and retrieve value from them.

Your Get-GUID.ps1 file will look like this:

$buildguid = (New-Guid).Guid$name = "Tom"Write-Output $buildguid | Out-File "buildguid.txt"Write-Output $name | Out-File "name.txt"

The output can be retrieved from the pipeline like this:

pipeline {    agent { label 'windows' }    stages {        stage("Populate variables")        {            steps {                script {                    def result = powershell returnStatus:true, script: '.\\Get-GUID.ps1'                }            }        }                stage("Display variables")        {            steps {                script {                    def guid = powershell returnStdout:true, script: 'Get-Content .\\buildguid.txt'                    def name = powershell returnStdout:true, script: 'Get-Content .\\name.txt'                    print guid.trim()                    print name.trim()                }            }        }    }}

Note: Don't forget to trim() your output when retrieved.