Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-60423

Powershell fails when the script starts with 'CmdletBinding' attribut

    XMLWordPrintable

Details

    Description

      Hi, 

       

      Since upgrade to the latest version of PowerShell Plugin (1.4) a lot of my scripts does not work any more.

      Especially when inline or script files starts with the 'CmdletBinding' attribut.

      [workspace] $ powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File C:\Users\ATHICS~1\AppData\Local\Temp\jenkins8862236051379677851.ps1

      Au caractŠre C:\Users\AthicService\AppData\Local\Temp\jenkins8862236051379677851.ps1:3 : 1

      + [CmdletBinding()]

      ~+ ~~~~~~~~~~~~~~~~~~

      Attribut inattendu ®ÿCmdletBindingÿ¯.

      Au caractŠre C:\Users\AthicService\AppData\Local\Temp\jenkins8862236051379677851.ps1:4 : 1

      + param([switch]$Elevated

      ~+ ~~~~~~

      Jeton inattendu ®ÿparamÿ¯ dans l'expression ou l'instruction.

          + CategoryInfo          : ParserError: ( [], ParentContainsErrorRecordException

          + FullyQualifiedErrorId : UnexpectedAttribute

       

      Build step 'PowerShell' marked build as failure

       

       

      As I have a lot of PS scripts like that, I have to downgrade to 1.3 version.

       

      Attachments

        Activity

          owenmehegan Owen Mehegan added a comment -

          froque OK, I have made you the default assignee for those two plugins. 

          owenmehegan Owen Mehegan added a comment - froque OK, I have made you the default assignee for those two plugins. 
          sup3rmark_ Mark Corsillo added a comment - - edited

          Hello! Just wanted to fan the embers here a bit.

          There's a very valid use case for including a param() block in a Powershell script in Jenkins which is broken by this force-setting of ErrorActionPreference. We can set our parameters by tying them to environment variables (which are set as part of parameterized builds) but still retain the ability to run the script with parameters outside of Jenkins without having to maintain a separate script.

          One example of how we typically do this on my end:

          param ( 
            [string]$foo = if ($ENV:foo) {$ENV:foo} else {'default'},
            [string]$bar= if ($ENV:bar) {$ENV:bar} else {'default'}
          )

          By forcing the ErrorActionPreference with no option to disable that, the param block no longer works because the param block needs to be the first thing defined in a Powershell script.

           

          sup3rmark_ Mark Corsillo added a comment - - edited Hello! Just wanted to fan the embers here a bit. There's a very valid use case for including a param() block in a Powershell script in Jenkins which is broken by this force-setting of ErrorActionPreference. We can set our parameters by tying them to environment variables (which are set as part of parameterized builds) but still retain the ability to run the script with parameters outside of Jenkins without having to maintain a separate script. One example of how we typically do this on my end: param ( [string]$foo = if ($ENV:foo) {$ENV:foo} else { ' default ' }, [string]$bar= if ($ENV:bar) {$ENV:bar} else { ' default ' } ) By forcing the ErrorActionPreference with no option to disable that, the param block no longer works because the param block needs to be the first thing defined in a Powershell script.  
          sup3rmark_ Mark Corsillo added a comment -

          Submitted a PR to correct this issue. First time doing a PR for a real repo, so apologies if I've done anything incorrectly, but https://github.com/jenkinsci/powershell-plugin/pull/18

           

          froque mind taking a look when you get a chance? 

          sup3rmark_ Mark Corsillo added a comment - Submitted a PR to correct this issue. First time doing a PR for a real repo, so apologies if I've done anything incorrectly, but https://github.com/jenkinsci/powershell-plugin/pull/18   froque  mind taking a look when you get a chance? 
          froque Filipe Roque added a comment -

          sup3rmark_ That code excerpt does not work for me

          $ cat test.ps1
          param (
            [string]$foo = if ($ENV:foo) {$ENV:foo} else {'default'},
            [string]$bar= if ($ENV:bar) {$ENV:bar} else {'default'}
          )
          $ pwsh -File test.ps1 
          ParserError: (...)/test.ps1:2
          Line |
             2 |    [string]$foo = if ($ENV:foo) {$ENV:foo} else {'default'},
               |                  ~
               | Missing expression after '='.
          
          
          froque Filipe Roque added a comment - sup3rmark_ That code excerpt does not work for me $ cat test.ps1 param ( [string]$foo = if ($ENV:foo) {$ENV:foo} else { ' default ' }, [string]$bar= if ($ENV:bar) {$ENV:bar} else { ' default ' } ) $ pwsh -File test.ps1 ParserError: (...)/test.ps1:2 Line | 2 | [string]$foo = if ($ENV:foo) {$ENV:foo} else { ' default ' }, | ~ | Missing expression after '=' .
          sup3rmark_ Mark Corsillo added a comment - - edited

          froque sorry, you're right. should be:

          param (
            [string]$foo = $(if ($ENV:foo) {$ENV:foo} else {'default'}),
            [string]$bar = $(if ($ENV:bar) {$ENV:bar} else {'default'})
          )
          

           

          sup3rmark_ Mark Corsillo added a comment - - edited froque  sorry, you're right. should be: param ( [string]$foo = $( if ($ENV:foo) {$ENV:foo} else { ' default ' }), [string]$bar = $( if ($ENV:bar) {$ENV:bar} else { ' default ' }) )  

          People

            froque Filipe Roque
            soruk Aleksander Kasprzyk
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated: