简体   繁体   中英

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 -

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")
            }
        }
    }
  }

Try

pwsh -command "&.\psscript.ps1"

If it requires it-

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM