Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

$ErrorActionPreference = "Stop" may have unintended consequences #38

Open
codykonior opened this issue Sep 8, 2016 · 3 comments
Open

Comments

@codykonior
Copy link

This line:

$runspace.powershell.EndInvoke($runspace.Runspace)

Means that if your session is set to stop on errors, and any of your jobs write an error, Invoke-Parallel will error out at the point it is receiving the data.

You might want to evaluate:

try { 
$runspace.powershell.EndInvoke($runspace.Runspace) 
} catch { 
Write-Error $_ -ErrorAction Continue
}

This still writes the error but doesn't abort the pipeline; it can also be stored for later when called using something like Invoke-Parallel -ErrorVariable MyErrorList

@oldlost
Copy link

oldlost commented Sep 13, 2016

Do you think this would invoke a similar issue with PoshRSJob module?

@yl10030270
Copy link

same issue

$ErrorActionPreference = "Stop"
 try { Write-Error "fsdfs" }  catch { Write-Host "First catch"} 
 1 | Invoke-Parallel -ImportModules -ImportVariables -ScriptBlock { 
        try {Write-Error "fsdfs"}
        catch {
            Start-Sleep -s 10
            Write-Host "Second catch"
        } 
    }

The first catch works , but invoke-parallel will not go into the second catch block.
instead the evaluation of "$runspace.Runspace.isCompleted" will immediately become true and go into following code block

                #check if there were errors
                    if ($runspace.powershell.Streams.Error.Count -gt 0) {
                        #set the logging info and move the file to completed
                        $log.status = "CompletedWithErrors"
                        Write-Host ($log | ConvertTo-Csv -Delimiter ";" -NoTypeInformation)[1]
                        foreach ($ErrorRecord in $runspace.powershell.Streams.Error) {
                            Write-Error -ErrorRecord $ErrorRecord
                        }
                    }

output : "4/25/2018 5:14:48 PM";"Removing:'1'";"0.0110033 seconds";"CompletedWithErrors";
The above section also have issue with Error stream check, it should use $runspace.powershell.HadErrors check in my opinion.
When the scriptblock throw someexceptionmessage , the error stream will not get populated, and the status will become "Completed" instead of "CompletedWithErrors". But the HadErrors will still get set.

I guess also should add ErrorActionPreference check like VerbosePreference check as following , although I don't understand why $VerbosePreference should be checked.

if ($VerbosePreference -eq 'Continue') {
                    [void]$PowerShell.AddScript( {$VerbosePreference = 'Continue'})
                }

@Hansson0728
Copy link

i did something like this to try and catch the inner error:

if ($obj.Value.Powershell.HadErrors -and ($obj.Value.Powershell.Streams.Error.count -eq 0))
{
  Try 
  {
    $Obj.Value.Powershell.endinvoke($Obj.Value.Handle)
   }
    catch
    {
      $errorstream = $_.Exception.InnerException.ErrorRecord
     }
   }
  else
  {
   $errorstream = $obj.Value.Powershell.Streams.Error.readall()
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants