CEamonn
CEamonn

Reputation: 925

Running a powershell script on via jenkins on a linux host

I have a powershell script cloned into my workspace from a bitbucket repository. If I log onto the jenkins server cli I can run it by doing

cd /var/lib/jenkins/workspace/powershell
pwsh
./psscript.ps1

This runs fine as expected, but when I try to run it via jenkins I get the error

/tmp/jenkins2117772455970634975.sh: line 3: ./psscript.ps1: Permission denied

My jenkins user is

user.name   netuser

and when I do a whoami from both the pwsh cli console and on the linux cli I get the same thing

PS /var/lib/jenkins/workspace/powershell> whoami
netuser

[netuser@server1]$ whoami
netuser

Showing jenkins user

[netuser@server1]$ ps axufwwww | grep 'jenkins\|java' -
netuser  31903  0.0  0.0 112660   980 pts/1    S+   22:15   0:00              \_ grep --color=auto jenkins\|java -

Upvotes: 2

Views: 5403

Answers (3)

codepen
codepen

Reputation: 429

Here is good reference how to use it in pipeline.

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

They have added support for powershell core. See here

https://github.com/jenkinsci/workflow-durable-task-step-plugin/blob/master/CHANGELOG.md

Here is how you can use it in your pipeline.

pipeline {
    agent any
    stages {
        stage ("PowershellDemo") {
            steps {
                pwsh ( returnStatus: true, script: "&.\psscript.ps1")
            }
        }
    }
  }

Upvotes: 3

Jacob Kucinic
Jacob Kucinic

Reputation: 115

Try

pwsh -command "&.\psscript.ps1"

If it requires it-

sudo pwsh -command "&.\psscript.ps1"

Upvotes: 1

Clayton Lewis
Clayton Lewis

Reputation: 394

I had issues getting a my first PS script to run via jenkins until I did the following. I had to save the entire path w/ file name into a variable and then call powershell to execute. Not sure if this will help if your situation though.

$File_Path_Name = $ENV:WORKSPACE + "\file.ps1"

Powershell -File $File_Path_Name

Upvotes: 0

Related Questions