Home > Software design >  How to choose which Powershell version Jenkins should use?
How to choose which Powershell version Jenkins should use?

Time:11-10

I am using enter image description here

The version I have running on my server is 1.7. The problem arises when I try to actually change the version of Powershell, since the example given uses a scripted pipeline, whereas my project uses a declarative one, so the built-in option for version selection is missing. How can I force Jenkins to use Powershell 7.2 instead of the default 5.*?

The only solution I can think of is using Powershell 5 to start Powershell 7 and to then run my .ps1 script like that, but that seems stupid.

CodePudding user response:

You can make it conditional. This script re-launches itself with version 7.2 only if the version is greater than 5.0.

if ($PSVersionTable.PSVersion -gt [Version]"5.0") {

powershell -Version 7.2 -File $MyInvocation.MyCommand.Definition

exit

}

'run some code'

Read-Host -Prompt "Scripts Completed : Press any key to exit"

CodePudding user response:

In both declarative and scripted pipeline you can use the pwsh step to run a script in PS 7.x.

Declarative:

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                pwsh '''
                    Write-Host "foo"
                '''
            }
        }
    }
}

Scripted:

node {
     pwsh '''
         Write-Host "foo"
     '''
}
  • Related