The attached patch fixes two issues with the powershell plugin:

      • The build now fails when the script has parse errors, which it the current version doesn't do. (Because powershell.exe doesn't exits with 0 on parse errors.)
      • It avoids ExecutionPolicy errors with the default ExecutionPolicy in place.

      The changes do mean it depends om PowerShell v2, I assume (but didn't test) it will fail with PowerShell v1. But I doubt anyone considers this an issue.

      I also made the plugin depend on Jenkins 1.450 because as is it didn't build with JDK 7.

          [JENKINS-15890] [PATCH] Powershell improvements

          Damien Finck added a comment -

          Hello,

          I tried your patch.
          I agree with you about ExcetuionPolicy ByPass.
          But for the exit code, I do not think this is a good solution. When a script is executed, it should be fully executed and not stop on the first error (as batch files). To stop a script, you must use "exit 1" and so fail the build.
          I found a better solution fot the exit code (the same than "Execute Windows batch command").

          I created a pull request with my changes.

          Damien

          Damien Finck added a comment - Hello, I tried your patch. I agree with you about ExcetuionPolicy ByPass. But for the exit code, I do not think this is a good solution. When a script is executed, it should be fully executed and not stop on the first error (as batch files). To stop a script, you must use "exit 1" and so fail the build. I found a better solution fot the exit code (the same than "Execute Windows batch command"). I created a pull request with my changes. Damien

          Code changed in jenkins
          User: Damien Finck
          Path:
          src/main/java/hudson/plugins/powershell/PowerShell.java
          http://jenkins-ci.org/commit/powershell-plugin/e81fe9920f4949657483e71c133c7045c3e96648
          Log:
          JENKINS-15890 Avoid ExecutionPolicy errors

          ByPass PowerShell ExecutionPolicy

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Damien Finck Path: src/main/java/hudson/plugins/powershell/PowerShell.java http://jenkins-ci.org/commit/powershell-plugin/e81fe9920f4949657483e71c133c7045c3e96648 Log: JENKINS-15890 Avoid ExecutionPolicy errors ByPass PowerShell ExecutionPolicy

          Code changed in jenkins
          User: Damien Finck
          Path:
          src/main/java/hudson/plugins/powershell/PowerShell.java
          http://jenkins-ci.org/commit/powershell-plugin/4f7c7b40f09d2a42b6d8407c7874836898058691
          Log:
          [FIXED JENKINS-15890] support of exit code

          PowerShell Plugin now support exit code to fail the build

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Damien Finck Path: src/main/java/hudson/plugins/powershell/PowerShell.java http://jenkins-ci.org/commit/powershell-plugin/4f7c7b40f09d2a42b6d8407c7874836898058691 Log: [FIXED JENKINS-15890] support of exit code PowerShell Plugin now support exit code to fail the build

          Code changed in jenkins
          User: Damien Finck
          Path:
          .gitignore
          README.md
          pom.xml
          src/main/java/hudson/plugins/powershell/PowerShell.java
          src/main/resources/index.jelly
          http://jenkins-ci.org/commit/powershell-plugin/b671a56671046bfb6c79d78cdfc938fd1f0cb374
          Log:
          Merge pull request #1 from damienfinck/master

          [FIXED JENKINS-15890] PowerShell improvements

          Compare: https://github.com/jenkinsci/powershell-plugin/compare/3ee3d3f79f3e...b671a5667104

          SCM/JIRA link daemon added a comment - Code changed in jenkins User: Damien Finck Path: .gitignore README.md pom.xml src/main/java/hudson/plugins/powershell/PowerShell.java src/main/resources/index.jelly http://jenkins-ci.org/commit/powershell-plugin/b671a56671046bfb6c79d78cdfc938fd1f0cb374 Log: Merge pull request #1 from damienfinck/master [FIXED JENKINS-15890] PowerShell improvements Compare: https://github.com/jenkinsci/powershell-plugin/compare/3ee3d3f79f3e...b671a5667104

          While I get your reasoning using $LastExitCode doesn't quite cover detecting errors in PowerShell. $LastExitCode will only be set after an executable has been run, it not set when a cmdlet fails (and might be unset when no executable was started in the script). On top of that the script might start multiple executables and this only fails the build when the last of those fails. Perhaps checking $errors could be useful, but error handling in PowerShell is a bit painful anyway.

          Arjan Veenstra added a comment - While I get your reasoning using $LastExitCode doesn't quite cover detecting errors in PowerShell. $LastExitCode will only be set after an executable has been run, it not set when a cmdlet fails (and might be unset when no executable was started in the script). On top of that the script might start multiple executables and this only fails the build when the last of those fails. Perhaps checking $errors could be useful, but error handling in PowerShell is a bit painful anyway.

          Hector Magnanao added a comment - - edited

          Can some describe how to apply this patch to the powershell plugin error for executionpolicy problems ? I don't see these 2 files under my Jenkins home directory. And does this patch work for Powershell 3.0 ?

          Hector Magnanao added a comment - - edited Can some describe how to apply this patch to the powershell plugin error for executionpolicy problems ? I don't see these 2 files under my Jenkins home directory. And does this patch work for Powershell 3.0 ?

          Can you please guide me how to apply this patch? I am using Jenkins ver. 1.591 on windows as Windows native package.

          I am new to jenkins if you can guide me.

          I don't see these 2 files under my Jenkins home directory.

          Where I can find the following file in Jenkins build directory?

          src/main/java/hudson/plugins/powershell/PowerShell.java

          Muhammad Saad Shahid Usmani added a comment - - edited Can you please guide me how to apply this patch? I am using Jenkins ver. 1.591 on windows as Windows native package. I am new to jenkins if you can guide me. I don't see these 2 files under my Jenkins home directory. Where I can find the following file in Jenkins build directory? src/main/java/hudson/plugins/powershell/PowerShell.java

          Arjan Veenstra added a comment - - edited

          You don't need to apply this patch, it's already in the latest release of the plugin.

          Arjan Veenstra added a comment - - edited You don't need to apply this patch, it's already in the latest release of the plugin.

          Tim Bradt added a comment -

          I just installed the plugin today (v1.2) and my job did not fail even though the Powershell errored:

          Started by user anonymous
          [EnvInject] - Loading node environment variables.
          Building on master in workspace C:\SPE\Jenkins\jobs\Test1\workspace
          [EnvInject] - Injecting environment variables from a build step.
          [EnvInject] - Injecting as environment variables the properties file path 'C:/SPE/Jenkins/DailyBuild/buildtime.properties'
          [EnvInject] - Variables injected successfully.
          [workspace] $ cmd /c call C:\Users\SPESER~1\AppData\Local\Temp\hudson868747164412287880.bat

          C:\SPE\Jenkins\jobs\Test1\workspace>rem echo BUILDTIME=1027 > C:\SPE\Jenkins\DailyBuild\buildtime.properties

          C:\SPE\Jenkins\jobs\Test1\workspace>exit 0
          [workspace] $ powershell.exe "& 'C:\Users\SPESER~1\AppData\Local\Temp\hudson7972185748419603015.ps1'"
          out-file : Could not find a part of the path 'C:\SPE\Jenkins\jobs\Test1\workspace%SE_R_MASTER%\buildtime.properties'.
          At C:\Users\speservice\AppData\Local\Temp\hudson7972185748419603015.ps1:2 char:1
          + Write-Output "BUILDTIME=$time" > %SE_R_MASTER%\buildtime.properties
          + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo : OpenError: ( [Out-File], DirectoryNotFoundException
          + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

          Finished: SUCCESS

          I don't like being locked in to being path relative to workspace (the windows batch command doesn't do this). Yes, I have already adjusted my path, but my primary issue is having the false positive. That makes it unusable in a production environment.

          Tim Bradt added a comment - I just installed the plugin today (v1.2) and my job did not fail even though the Powershell errored: Started by user anonymous [EnvInject] - Loading node environment variables. Building on master in workspace C:\SPE\Jenkins\jobs\Test1\workspace [EnvInject] - Injecting environment variables from a build step. [EnvInject] - Injecting as environment variables the properties file path 'C:/SPE/Jenkins/DailyBuild/buildtime.properties' [EnvInject] - Variables injected successfully. [workspace] $ cmd /c call C:\Users\SPESER~1\AppData\Local\Temp\hudson868747164412287880.bat C:\SPE\Jenkins\jobs\Test1\workspace>rem echo BUILDTIME=1027 > C:\SPE\Jenkins\DailyBuild\buildtime.properties C:\SPE\Jenkins\jobs\Test1\workspace>exit 0 [workspace] $ powershell.exe "& 'C:\Users\SPESER~1\AppData\Local\Temp\hudson7972185748419603015.ps1'" out-file : Could not find a part of the path 'C:\SPE\Jenkins\jobs\Test1\workspace%SE_R_MASTER%\buildtime.properties'. At C:\Users\speservice\AppData\Local\Temp\hudson7972185748419603015.ps1:2 char:1 + Write-Output "BUILDTIME=$time" > %SE_R_MASTER%\buildtime.properties + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OpenError: ( [Out-File] , DirectoryNotFoundException + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand Finished: SUCCESS I don't like being locked in to being path relative to workspace (the windows batch command doesn't do this). Yes, I have already adjusted my path, but my primary issue is having the false positive. That makes it unusable in a production environment.

          Just for info: note that this issue has been fixed in GitHub, but a new release has not yet been made which includes this fix.

          The current version, 1.2, is from 2009, so does not include this fix.

          Christopher Orr added a comment - Just for info: note that this issue has been fixed in GitHub, but a new release has not yet been made which includes this fix. The current version, 1.2, is from 2009, so does not include this fix.

            Unassigned Unassigned
            avee Arjan Veenstra
            Votes:
            3 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: