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

Powershell script execution broken in powershellHelper.ps1

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Component/s: durable-task-plugin
    • Labels:
      None
    • Environment:
      Win2008R2 master, Win2016 agent. Jenkins 2.117 and earlier.
    • Similar Issues:

      Description

      Powershell script execution is broken, possibly after upgrading to durable task plugin 1.22.

      11:15:08 out-lineoutput : Object reference not set to an instance of an object.
      11:15:08 At C:\Builds\home\workspace\INTG_Lab_Installation_Script_Test\workspace@tmp\durable-19f0c27a\powershellHelper.ps1:25 char:7
      11:15:08 +       $Input | Out-String -Stream -Width 192 | ForEach-Object { $Writer.WriteLin ...
      11:15:08 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      11:15:08     + CategoryInfo          : NotSpecified: (:) [out-lineoutput], NullReferenc    eException
      11:15:08     + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShe    ll.Commands.OutLineOutputCommand
      11:15:08  

      Reverting to 1.17 broke everything

      Running in Durability level: MAX_SURVIVABILITY
      [Pipeline] End of Pipeline
      java.lang.NoSuchMethodError: No such DSL method 'node' found among steps [addBadge, addErrorBadge, addHtmlBadge, addInfoBadge, addShortText, addWarningBadge, ansiColor, archive, build, catchError, checkout, createSummary, deleteDir, dir, echo, error, fileExists, getContext, git, input, isUnix, jiraComment, jiraIssueSelector, jiraSearch, junit, library, libraryResource, load, mail, milestone, p4, p4approve, p4publish, p4sync, p4tag, p4unshelve, parallel, properties, publishHTML, pwd, readFile, readTrusted, removeBadges, removeHtmlBadges, removeSummaries, resolveScm, retry, sleep, stage, stash, step, svn, timeout, timestamps, tm, tool, unarchive, unstash, waitUntil, withContext, withCredentials, withEnv, wrap, writeFile] or symbols [absolute, agent, all, always, ant, antFromApache, antOutcome, antTarget, apiToken, approve, architecture, archiveArtifacts, artifactManager, authorizationMatrix, autoClean, batchFile, bitbucket, booleanParam, buildButton, buildDiscarder, bzt, caseInsensitive, caseSensitive, certificate, checkstyle, choice, choiceParam, ciGame, cleanup, clientSpec, clock, cloud, command, credentials, cron, crumb, defaultView, demand, depotSource, disableConcurrentBuilds, disableResume, dockerCert, downloadSettings, downstream, dumb, durabilityHint, envVars, file, fileCopyOperation, fileCreateOperation, fileDeleteOperation, fileDownloadOperation, fileJoinOperation, fileOperations, fileParam, filePath, filePropertiesToJsonOperation, fileRenameOperation, fileTransformOperation, fileUnTarOperation, fileUnZipOperation, fileZipOperation, fingerprint, fishEye, flushOnly, folderCopyOperation, folderCreateOperation, folderDeleteOperation, folderRenameOperation, forceClean, frameOptions, freeStyle, freeStyleJob, fromScm, fromSource, git, github, githubPush, globalLib, gradle, graphClean, graphSource, headRegexFilter, headWildcardFilter, hyperlink, hyperlinkToModels, incremental, inheriting, inheritingGlobal, installSource, jdk, jdkInstaller, jgit, jgitapache, jnlp, jobName, label, lastDuration, lastFailure, lastGrantedAuthorities, lastStable, lastSuccess, legacy, legacySCM, list, local, location, logRotator, loggedInUsersCanDoAnything, manualSpec, masterBuild, matrix, maven, maven3Mojos, mavenErrors, mavenMojos, mavenWarnings, modernSCM, multiBranch, multiGraph, multiStreams, multiSwarm, myView, nodeProperties, nonInheriting, nonStoredPasswordParam, none, note, openGrok, overrideIndexTriggers, p4Trigger, p4Web, p4cleanup, p4groovy, p4sync, paneStatus, parallel, parameters, password, pathFilter, pattern, perfReport, perforce, performanceReport, performanceTest, pipelineTriggers, plainText, plugin, pollSCM, previewOnly, previous, projectNamingStrategy, proxy, publish, queueItemAuthenticator, quietPeriod, relative, remotingCLI, run, runParam, schedule, scmRetryCount, scriptApprovalLink, search, security, shell, shelve, slave, sourceRegexFilter, sourceWildcardFilter, specFileSpec, sshUserPrivateKey, stackTrace, standard, staticSpec, status, streamSource, streamSpec, string, stringParam, submit, swapSpace, swarm, syncOnly, templateSource, templateSpec, testCase, text, textParam, ticket, ticketMode, tmpSpace, toolLocation, trust, unsecured, unshelve, upstream, user, userFilter, usernameColonPassword, usernamePassword, viewFilter, viewPattern, viewsTabBar, warnings, weather, withAnt, zfs, zip] or globals [currentBuild, env, manager, params, scm]
      	at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:176)
      	at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:108)
      	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:48)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
      	at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
      	at WorkflowScript.run(WorkflowScript:1)
      	at ___cps.transform___(Native Method)
      	at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:57)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:109)
      	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:82)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      	at java.lang.reflect.Method.invoke(Unknown Source)
      	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
      	at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46)
      	at com.cloudbees.groovy.cps.Next.step(Next.java:83)
      	at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
      	at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
      	at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
      	at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
      	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
      	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
      	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
      	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
      	at java.util.concurrent.FutureTask.run(Unknown Source)
      	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
      	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
      	at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
      	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
      	at java.util.concurrent.FutureTask.run(Unknown Source)
      	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      	at java.lang.Thread.run(Unknown Source)
      Finished: FAILURE
      

      Nothing else was changed. Restoring 1.22 re-introduced the original error.

        Attachments

          Activity

          Hide
          ghs1 g hs1 added a comment - - edited

          Note that the PowerShell script itself still runs correctly outside of Jenkins. There are no spaces in the job name (JENKINS-48415)

          Show
          ghs1 g hs1 added a comment - - edited Note that the PowerShell script itself still runs correctly outside of Jenkins. There are no spaces in the job name ( JENKINS-48415 )
          Hide
          gabloe Gabriel Loewen added a comment -

          It looks like this is an edge case that is triggered when streaming console output from a powershell script cannot be interpreted as serializable data.  I will work on a fix for this.

          Show
          gabloe Gabriel Loewen added a comment - It looks like this is an edge case that is triggered when streaming console output from a powershell script cannot be interpreted as serializable data.  I will work on a fix for this.
          Hide
          gabloe Gabriel Loewen added a comment - - edited

          This edge case seems to be happening only with powershell format cmdlets, such as Format-Table, etc...  Are you using one of these cmdlets?  You can fix it temporarily by piping any calls to Format-* cmdlets to Out-String:

          e.g.

          $Data | Format-Table | Out-String

          Meanwhile, it should be fixed in my PR: https://github.com/jenkinsci/durable-task-plugin/pull/70

           

          Show
          gabloe Gabriel Loewen added a comment - - edited This edge case seems to be happening only with powershell format cmdlets, such as Format-Table, etc...  Are you using one of these cmdlets?  You can fix it temporarily by piping any calls to Format-* cmdlets to Out-String: e.g. $Data | Format-Table | Out- String Meanwhile, it should be fixed in my PR:  https://github.com/jenkinsci/durable-task-plugin/pull/70  
          Hide
          ghs1 g hs1 added a comment -

          Thank you. It doesn't appear so. Here's the script:

          Param (
              [Alias("Host")]
              [string]$VIServer = "*******************",
              [string]$vmname = "*******************",
              [string]$admin = "*******************",
              [string]$pathToCredentials
          )
          
          #You must change these values to securely save your credential files
          $key = *******************
          
          Function Get-Credentials {
              Param (
                  [String]$authUser = $env:USERNAME,
                  [string]$pathToCred
              )
          
              #Build the path to the credential file
              $credFile = $authUser.Replace("\","~")
              If (-not $pathToCred){
                  $pathToCred = Split-Path $MyInvocation.MyCommand.Path
              }
              $file = Join-Path -Path $pathToCred -ChildPath "\Credentials-$credFile.crd"
              #And find out if it's there, if not create it
              If(-not (Test-Path $file)){
                  (Get-Credential $authUser).Password | ConvertFrom-SecureString -Key $key | Set-Content $file
              }
              #Load the credential file
              $password = Get-Content $file | ConvertTo-SecureString -Key $key
              $authUser = (Split-Path $file -Leaf).Substring(12).Replace("~","\")
              $authUser = $authUser.Substring(0,$authUser.Length - 4)
              $credential = New-Object System.Management.Automation.PsCredential($authUser,$password)
              Return $credential
          }
          
          If(-not $pathToCredentials){
              $pathToCredentials = Split-Path $MyInvocation.MyCommand.Path
          }
          
          Clear-Host
          
          $svr = "*******************"
          $snapshot = "*******************"
          $cred = Get-Credentials $admin $pathToCredentials
          Try{
              Write-Output "Connecting to vSphere server..."
              $vc = Connect-VIServer $VIServer -Credential $cred -ErrorAction Stop 3>$null
              Get-VM -Server $vc -Name $svr | Get-Snapshot | Select VM, Created, Name, SizeMB | FT
              Write-Output "Restoring snapshot..."
              $vm = Get-VM -Server $vc -Name $vmname
              $snapshot = Get-Snapshot -VM $vm -Name $snapshot
              Set-VM -VM $vm -Snapshot $snapshot -Confirm:$false
          }
          Catch{
              Throw "Error connecting to $VIServer because ""$($Error[1])"""
          }
          # Exit with 0 unless an exception is thrown.
          # Prevents returned values greater than 1 failing the build unnecessarily.
          exit 0
          

          It uses VMware's PowerCLI to restore a VM from a snapshot.

          Is there any way I can test your fix without waiting weeks or months for a release?

          Thanks.

          Show
          ghs1 g hs1 added a comment - Thank you. It doesn't appear so. Here's the script: Param (     [Alias( "Host" )]     [string]$VIServer = "*******************" ,     [string]$vmname = "*******************" ,     [string]$admin = "*******************" ,     [string]$pathToCredentials ) #You must change these values to securely save your credential files $key = ******************* Function Get-Credentials {     Param (         [ String ]$authUser = $env:USERNAME,         [string]$pathToCred     )     #Build the path to the credential file     $credFile = $authUser.Replace( "\" , "~" )     If (-not $pathToCred){         $pathToCred = Split-Path $MyInvocation.MyCommand.Path     }     $file = Join-Path -Path $pathToCred -ChildPath "\Credentials-$credFile.crd"     #And find out if it's there, if not create it     If(-not (Test-Path $file)){         (Get-Credential $authUser).Password | ConvertFrom-SecureString -Key $key | Set-Content $file     }     #Load the credential file     $password = Get-Content $file | ConvertTo-SecureString -Key $key     $authUser = (Split-Path $file -Leaf).Substring(12).Replace( "~" , "\" )     $authUser = $authUser.Substring(0,$authUser.Length - 4)     $credential = New- Object System .Management.Automation.PsCredential($authUser,$password)     Return $credential } If(-not $pathToCredentials){     $pathToCredentials = Split-Path $MyInvocation.MyCommand.Path } Clear-Host $svr = "*******************" $snapshot = "*******************" $cred = Get-Credentials $admin $pathToCredentials Try{     Write-Output "Connecting to vSphere server..."     $vc = Connect-VIServer $VIServer -Credential $cred -ErrorAction Stop 3>$ null     Get-VM -Server $vc -Name $svr | Get-Snapshot | Select VM, Created, Name, SizeMB | FT     Write-Output "Restoring snapshot..."     $vm = Get-VM -Server $vc -Name $vmname     $snapshot = Get-Snapshot -VM $vm -Name $snapshot     Set-VM -VM $vm -Snapshot $snapshot -Confirm:$ false } Catch{     Throw "Error connecting to $VIServer because " "$($Error[1])" "" } # Exit with 0 unless an exception is thrown. # Prevents returned values greater than 1 failing the build unnecessarily. exit 0 It uses VMware's PowerCLI to restore a VM from a snapshot. Is there any way I can test your fix without waiting weeks or months for a release? Thanks.
          Hide
          gabloe Gabriel Loewen added a comment - - edited

          The problematic line of code is this one:

          Get-VM -Server $vc -Name $svr | Get-Snapshot | Select VM, Created, Name, SizeMB | FT
          

          Here, FT is an alias for Format-Table, which is what is causing the exception to be thrown. Without taking my fix you could modify that line slightly:

          Get-VM -Server $vc -Name $svr | Get-Snapshot | Select VM, Created, Name, SizeMB | FT | Out-String
          

          Which should get past the issue. If you want to build and use my latest bits you can always checkout my repo and build it using Maven, and then install the HPI file in your Jenkins instance.

           

          Show
          gabloe Gabriel Loewen added a comment - - edited The problematic line of code is this one: Get-VM -Server $vc -Name $svr | Get-Snapshot | Select VM, Created, Name, SizeMB | FT Here, FT is an alias for Format-Table , which is what is causing the exception to be thrown. Without taking my fix you could modify that line slightly: Get-VM -Server $vc -Name $svr | Get-Snapshot | Select VM, Created, Name, SizeMB | FT | Out- String Which should get past the issue. If you want to build and use my latest bits you can always checkout my repo and build it using Maven, and then install the HPI file in your Jenkins instance.  
          Hide
          ghs1 g hs1 added a comment -

          Thank you for your help and for investigating the problem. I'm not set up for Maven so I have applied your workaround and the script is now working.

          Show
          ghs1 g hs1 added a comment - Thank you for your help and for investigating the problem. I'm not set up for Maven so I have applied your workaround and the script is now working.
          Hide
          didried Edgars Didrihsons added a comment -

          How is this Fixed when I still encounter it today with Jenkins 2.138.3 and durable-task-plugin 1.27 ? Adding Out-String does help though so thanks for that.

          Show
          didried Edgars Didrihsons added a comment - How is this Fixed when I still encounter it today with Jenkins 2.138.3 and durable-task-plugin 1.27 ? Adding Out-String does help though so thanks for that.

            People

            • Assignee:
              gabloe Gabriel Loewen
              Reporter:
              ghs1 g hs1
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: