Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Hudson CI to execute a Powershell script?

I'm using Hudson version 1.324 for CI and have a couple of issues:

Environment:

  • Windows Server 2008
  • Powershell v1.0
  • Hudson 1.324 running as a service
  • Hudson Powershell Plugin installed
  • Psake (aka. "Powershell Make/Rake" available from Github) 0.23 (All current/latest versions as of this initial post)

I have a Powershell (PS) script that works to compile, run NUnit tests, and if successful, create a 7z file of the output. The PS script works from the command line, on both my local development box as well as the CI server where Hudson is installed.

1) Execution Policy with Powershell.

I initially ran a PS console on the server, ran Set-ExecutionPolicy Unrestricted, which allows any script to be run. (Yes, I realize the security concerns here, I'm trying to get something to work and Unrestricted should remove the security issues so I can focus on other problems.)

[This worked, and allowed me to fire off the PS build script from Hudson yesterday. I then encountered another problem, but we'll discuss that more in item #2.]

Once Hudson could fire off a PS script, it complained with the following error:

"C:\Windows\system32\WindowsPowerShell\v1.0\powershell "& 'OzSystems.Tools\psake\psake.ps1' '.\oz-build.ps1'" The term 'OzSystems.Tools\psake\psake.ps1' is not recognized as a cmdlet, funct ion, operable program, or script file. Verify the term and try again. At line:1 char:2 + & <<<< 'OzSystems.Tools\psake\psake.ps1' '.\oz-build.ps1'"

Using the same command line, I am able to successfully execute the PS script from the command line manually. However Hudson is unable to get PS to do the same. After looking at additional PS documentation I also tried this:

"& 'OzSystems.Tools\psake\psake.ps1' '.\oz-build.ps1'"

and got a similar error. There does not appear to be any documentation for the Powershell plugin for Hudson. I've gone through all the Powershell plugin files and don't see anything that's configurable. I can't find a log file for Hudson to get additional information.

Can anyone help me past this?

2) I spent yesterday wrestling with #1. I came in this AM and tried to dig in again, after restarting the Hudson server/service, and now it appears that the ExecutionPolicy has been reset to Restricted. I did what worked yesterday, opened a PS console and Set-ExecutionPolicy to Unrestricted. It shows Unrestricted in the PS console, but Hudson says that it doesn't have rights to execution PS scripts. I reopened a new PS console and confirmed that the ExecutionPolicy is still Unrestriced -- it is. But Hudson evidently is not aware of this change. Restarting Hudson service again does not change Hudson's view of the policy.

Does anyone know what's going on here?

Thanks, Derek

like image 784
user178557 Avatar asked Sep 24 '09 16:09

user178557


People also ask

How do I run a PowerShell script execution?

In File Explorer (or Windows Explorer), right-click the script file name and then select "Run with PowerShell". The "Run with PowerShell" feature starts a PowerShell session that has an execution policy of Bypass, runs the script, and closes the session.

How do I run a PowerShell script from a PowerShell script?

To open the PowerShell console, click on the Start button (or search button), type powershell, and click Run as Administrator. To run a script in the PowerShell console, you can either: Use the full path to script, like: C:\TEMP\MyNotepadScript. ps1.


2 Answers

I just ran into the problem of running powershell scripts in hudson. The thing is that you are running a 32-bit process of Java, and you've configured Hudson for 64-bit but not for 32-bit. See the following thread we created at microsoft.

http://social.technet.microsoft.com/Forums/en/winserverpowershell/thread/a9c08f7e-c557-46eb-b8a6-a19ba457e26d

If your lazy. 1. Start powershell (x86) from the start menu as administrator 2. Set the execution policy to remotesigned

Run this once and your homefree.

like image 157
Morten Avatar answered Oct 28 '22 07:10

Morten


When Running PowerShell from a scheduled task or Hudson you want to:

  1. Specify the -ExecutionPolicy parameter (in your case: -Ex Unrestricted)
  2. Specify that command using either -Command { ... } or -File NOT BOTH and not without specifying which you mean.

Try this (except that I don't recommend using relative paths):

PowerShell.exe -Ex Unrestricted -Command "C:\Path\To\OzSystems.Tools\psake\psake.ps1" ".\oz-build.ps1"

To be clear, this will work too:

PowerShell.exe -Ex Unrestricted -Command "&{&'OzSystems.Tools\psake\psake.ps1' '.\oz-build.ps1'}"

The first string after -Command is interpreted as THE NAME OF A COMMAND, and every parameter after that is just passed to that command as a parameter. The string is NOT a script, it's the name of a command (in this case, a script file)... you cannot put "&'OzSystems.Tools\psake\psake.ps1'" but you can put "OzSystems.Tools\psake\psake.ps1" even if it has spaces.

To quote from the help (run PowerShell -?) emphasis mine:

-Command

Executes the specified commands (and any parameters) as though they were typed at the Windows PowerShell command prompt, and then exits, unless NoExit is specified. The value of Command can be "-", a string. or a script block.

If the value of Command is "-", the command text is read from standard input.

If the value of Command is a script block, the script block must be enclosed in braces ({}). You can specify a script block only when running PowerShell.exe in Windows PowerShell. The results of the script block are returned to the parent shell as deserialized XML objects, not live objects.

If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments.

like image 44
Jaykul Avatar answered Oct 28 '22 07:10

Jaykul