Beniamino_Baggins Beniamino_Baggins -4 years ago 417
PowerShell Question

Run powershell command on slave - Jenkins

I'm getting this console output when running a Jenkins job on a slave. The job should create a file and put it in


Here is the job powershell command:

Get-Service | Export-CSV c:\foo\service.csv

Note the word “bypass” in this console output. I cannot get past it. I think it is the issue:

[AnonymousJWTApi] $ powershell.exe -NonInteractive -ExecutionPolicy
ByPass "&

I found online that the error happens when running a script sitting on a master, on a slave.

So I manually created the script on the slave and put it in
. Then in Jenkins (master) this command gets run on the slave:
to invoke the script to run directly from the slave.

Im still getting the same console output…something about “bypass” seems to be still causing it to not work. The csv file is not being put in the foo folder. Is it the bypass that is preventing it from working, and if so, what is the solution?

And the full console output:

Started by user anonymous
Building remotely on slave1 in workspace C:\Jenkins\Master\workspace\AnonymousJWTApi
> git.exe rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git.exe config remote.origin.url C:\Users\xxx\Source\Repos\AnonymousJWTApi # timeout=10
Fetching upstream changes from C:\Users\xxx\Source\Repos\AnonymousJWTApi
> git.exe --version # timeout=10
> git.exe -c core.askpass=true fetch --tags --progress C:\Users\xxx\Source\Repos\AnonymousJWTApi +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen 1 remote branch
Checking out Revision a7087f81af855cc96b8763a5ec66b96c19a44a30 (origin/master)
> git.exe config core.sparsecheckout # timeout=10
> git.exe checkout -f a7087f81af855cc96b8763a5ec66b96c19a44a30
> git.exe rev-list a7087f81af855cc96b8763a5ec66b96c19a44a30 # timeout=10
[AnonymousJWTApi] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\xxx\AppData\Local\Temp\hudson1582303083838020200.ps1'"
Finished: SUCCESS

EDIT: The file is getting saved to the master... :P

It seems the slave is not intended to host the PowerShell scripts or the outputs of them, which I was not aware of.

Answer Source

-ExecutionPolicy Bypass ensures that the script file can be run regardless of the execution policy in place for the computer or user. It is correct, and it is not the cause of your issue.

The problem may be permissions.

The Jenkins slave agent is running as a certain user; either the user you used to start it, or if it's running as a service, then it will be the service account.

The user may not have permission to write to C:\Foo, so you can check that.

Another way to test this is to write to the workspace directory, so do something like this:

Get-Service | Export-CSV $env:WORKSPACE\service.csv

That should work. If it does, then change the permission on the C:\Foo directory, or run the slave as an account that already has permission.

Here's a way you might be able to tell both which user is running the service, and where the workspace directory is:

Get-ChildItem env:\ | Out-String | Set-Content -Path $env:USERPROFILE\env.txt

This will write a file called env.txt to the profile of the user running the script, containing all of the environment variables (including %WORKSPACE%). You'll just have to browse through each user profile to see where it ends up, and then you'll also know the user.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download