Coder Social home page Coder Social logo

Comments (3)

oldlost avatar oldlost commented on June 17, 2024

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

from invoke-parallel.

yl10030270 avatar yl10030270 commented on June 17, 2024

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'})
                }

from invoke-parallel.

Hansson0728 avatar Hansson0728 commented on June 17, 2024

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()
  }

from invoke-parallel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.