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

Jenkins Powershell wrapper breaks valid Powershell scripts

XMLWordPrintable

      The following script fails when run in Jenkins:

      node("windows") {
        powershell('''\
          function Invoke-NativeCommand {
            # Do something useful here
          }
      
          function New-GitInvoker([string]$Path) {
            return {
              Invoke-NativeCommand git `-C $Path @args
            }.GetNewClosure()
          }
      		
          $git = New-GitInvoker 'testRepoPath'
          & $git fetch `-p ssh://someUrl
          '''.stripIndent()
        )
      }
      

      The error is:

      powershell.exe : Invoke-NativeCommand : The term 'Invoke-NativeCommand' is not recognized as the name of a cmdlet, function, script
      
      At D:\Jenkins\workspace\<job_name>@tmp\durable-38d496fa\powershellWrapper.ps1:3 char:1
      + & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (Invoke-NativeCo...nction, script :String) [], RemoteException
          + FullyQualifiedErrorId : NativeCommandError
      file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
      
      At D:\Jenkins\workspace\<job_name>@tmp\durable-38d496fa\powershellScript.ps1:7 char:3
      +         Invoke-NativeCommand git `-C $Path @args
      +         ~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : ObjectNotFound: (Invoke-NativeCommand:String) [], ParentContainsErrorRecordException
          + FullyQualifiedErrorId : CommandNotFoundException
      

      When run directly using PowerShell, the same script runs to completion without error.  Perhaps the wrapper messes up closures?

      A workaround exists - simply write the powershell script to a file and run it directly using powershell in the "bat" pipeline command:

      bat 'powershell -NoProfile -File myscript.ps1'

      Jenkins 2.289.1
      durable-task plugin version 1.37
      Pipeline: Nodes and Processes 2.39

            Unassigned Unassigned
            timpatt Tim Patterson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: