2

I am using Jenkins for deployment. I wanted to run a powershell script that copies some files. I have installed "Windows Powershell" plugin for jenkins that will execute the powershell script. My Powershell script looks like below

param (
     [string] $targetEnvironment,
     [switch] $WhatIf,
     [switch] $Compare)

    try
    {
       #do smething here
    }
    catch
    {
    }

This script is working fine when i execute it manually. However when run the script using the following command in plugin's input window i get error

enter image description here

Note that, if i copied the "deploymentscript" folder to "C:\deploymentscript" and then change the path in plugin's window then the job runs fine. It doesnt work when its executing under program files

The error im getting is

First time build. Skipping changelog.
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "&   'C:\Users\CODESC~1\AppData\Local\Temp\hudson2119904511537985474.ps1'"
At C:\Users\username\AppData\Local\Temp\hudson2119904511537985474.ps1:1   char:119
+ ... etaTaskar.ps1' -targetEnvironment demo
+                    ~~~~~~~~~~~~~~~~~~
Unexpected token '-targetEnvironment' in expression or statement.
At C:\Users\username\AppData\Local\Temp\hudson2119904511537985474.ps1:1    char:138
+ ... getEnvironment demo
+                    ~~~~
Unexpected token 'demo' in expression or statement.
+ CategoryInfo          : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken

Build step 'Windows PowerShell' marked build as failure
Finished: FAILURE

EDIT1
@petrik You are right, the space in the "Program Files (x86)" folder causing the issue. The solution is to use the following foormat

 &("C:\Program Files (x86)\Jenkins\jobs\MyJob\workspace\crconfig\deploymentscript\Deploy.ps1") -targetEnvironment demo

and that works.

Now based on the article here if the powershell scrit fails i want the Jenkin's job to fail too, So i have tell Hudson to call the powershell script form a windows batch task.

 powershell "& {&('C:\Program Files (x86)\Jenkins\jobs\MyJob\workspace\crconfig\deploymentscript\Deploy.ps1') -targetEnvironment $Env:EnvironmentParam; exit $lastexitcode }"

I am not sure if i really need to this in my case ??

6
  • My guess would be that it's not the fact it runs from 'Program Files' but the fact that it runs from a directory path that has spaces in it. What happens if you manually execute that command (the whole command as jenkins has it)? Commented Mar 3, 2016 at 19:09
  • 1
    Another option is to do something like: $script = 'c:\Program Files (x86)\Jenkins\jobs\MyJob\workspace\crconfig\deploymentscript\deploy.ps1';& $script -targetEnvironment demo. Commented Mar 3, 2016 at 19:11
  • @petrik you are right if i run the command from C drive manually i get the same error like PS C:\> "C:\Program Files (x86)\Jenkins\jobs\MyJob\workspace\crconfig\deploymentscript\Deploy.ps1" -targetEnvironment demo and i used double quote. So whats the solution? Commented Mar 3, 2016 at 19:54
  • 1
    Did you try the potential solution I suggested in my second comment, i.e. putting the path in a variable and then using the &-character to invoke the script? Commented Mar 3, 2016 at 22:47
  • Thanks, please see my edit1 above Commented Mar 4, 2016 at 1:28

2 Answers 2

1

So in this case the problem is caused by the fact that the 'Program Files' path has spaces in it. Putting the path to the script into a variable which can then be executed using the call operator.

In order to ensure that Jenkins fails the build if the Powershell script fails several steps need to be taken.

  1. Use the Jenkins powershell plugin. It will pass the powershell exit code on to Jenkins
  2. Set the $ErrorActionPreference to 'Stop' as early as possible. This forces Powershell to halt when exceptions are thrown. The default behaviour is to report the error but then continue.

The script in the Jenkins configuration could look like this:

$ErrorActionPreference = 'Stop'
$scriptPath = 'c:\Program Files (x86)\Jenkins\jobs\MyJob\workspace\crconfig\deploymentscript\deploy.ps1'
& $script -targetEnvironment 'demo'

This should make Jenkins run the script correctly and report an error and stop the build if the script fails.

Sign up to request clarification or add additional context in comments.

Comments

0

I did two things to resolve issue:

1) Appended the path to powerscript "c:\Users\\Documents\WindowsPowerShell\profile.ps1" to path variable of system properties.
2) Opened Powershell with administrative permissions and executed: "Set-ExecutionPolicy Unrestricted"

Then restarted windows machine. When i reconnected it to jenkins, my powershell just worked fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.