Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a PowerShell script as a job in Jenkins

This sounds like a simple question, but I haven't been able to work it out after looking online. I basically want to execute a PowerShell script (e.g. script.ps1) in Jenkins and report success/failure.

Try 1: Run following as "Execute Windows Batch Command"

powershell -File c:\scripts\script.ps1 

This starts up as expected, but it quits after a few seconds.

Try 2: Run following as "Execute Windows Batch Command"

powershell -NoExit -File c:\scripts\script.ps1 

This runs the whole script successfully, but it never stops. I had to manually abort the script.

like image 479
Danish Avatar asked May 04 '12 16:05

Danish


People also ask

How do I run a PowerShell script as administrator in Jenkins?

msc and right click on Jenkins. Then click Properties , go to Logon tab, check mark This account and enter username and password which has admin privileges. Stop and Start Jenkins service. From now on when you will run powershell commands in Jenkins, they will be run as administrator.

Does Jenkins use PowerShell?

Jenkins PowerShell PluginIntegrates with PowerShell by allowing you to directly write PowerShell scripts into the text box in Jenkins. Other than that, this plugin works pretty much like the standard shell script support.

Can PowerShell be used for automation?

PowerShell can be used to automate tasks such as user management, CI/CD, managing cloud resources and much more. You'll learn to run commands, how to learn more about PowerShell and additionally to create and run script files.


2 Answers

Well, there is a PowerShell plugin, which is wrapping the shell anyway. I use this on my server, executing scripts in standard notation:

powershell -File test001.ps1 

It works without any quirks.

like image 68
Łukasz Rżanek Avatar answered Sep 24 '22 11:09

Łukasz Rżanek


I found the problem came down to PowerShell managing the execution policy separately for 32-bit and 64-bit versions. Checkout http://www.gregorystrike.com/2011/01/27/how-to-tell-if-powershell-is-32-bit-or-64-bit/ to try launching both. Run Get-ExecutionPolicy in both and you will see they are different.

At least with version 1.4, it appears the plugin ends up using the 32-bit process.

like image 21
Adam Hewitt Avatar answered Sep 21 '22 11:09

Adam Hewitt