LP13
LP13

Reputation: 34189

jenkins cannot execute powershell under program files

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 ??

Upvotes: 2

Views: 3358

Answers (2)

Munish Mehta
Munish Mehta

Reputation: 403

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.

Upvotes: 0

Petrik
Petrik

Reputation: 1981

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.

Upvotes: 1

Related Questions