How to pass a parameter to a powershell script in a jenkins pipeline How to pass a parameter to a powershell script in a jenkins pipeline powershell powershell

How to pass a parameter to a powershell script in a jenkins pipeline


Try this:

node {    powershell '''        $VAR_A = 'test'        Write-Host "My result: '$VAR_A'"    '''    withEnv(["VAR_A=envtest"]) {        powershell '''            Write-Host "My result is empty: '$VAR_A'"            Write-Host "My env result: '$env:VAR_A'"        '''    }}

The output is:

My result: 'test'My result is empty: ''My env result: 'envtest'

This was tested on Jenkins 2.73.1.

Note that:

  • $VAR_A = 'test' is declared within the first powershell '''...'''
  • env: is required to access environment variables (see about_Environment_Variables in the Microsoft Docs)