Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:04:50+00:00 2026-05-28T00:04:50+00:00

I have the following script, $createZip = { Param ([String]$source, [String]$zipfile) Process { echo

  • 0

I have the following script,

$createZip = {
    Param ([String]$source, [String]$zipfile)
    Process { 
        echo "zip: $source`n     --> $zipfile"
        throw "test"
    }
}

try {
    Start-Job -ScriptBlock $createZip -ArgumentList "abd", "acd"  
    echo "**Don't reach here if error**"
    LogThezippedFile
}
catch {
    echo "Captured: "
    $_ | fl * -force
}
Get-Job | Wait-Job 
Get-Job | receive-job 
Get-Job | Remove-Job 

However, the exception raised in another powershell instance cannot be captured. What’s the best way to capture the exception?

Id              Name            State      HasMoreData     Location             Command                  
--              ----            -----      -----------     --------             -------                  
343             Job343          Running    True            localhost            ...                      
**Don't reach here if error**
343             Job343          Failed     True            localhost            ...                      
zip: abd
     --> acd
Receive-Job : test
At line:18 char:22
+ Get-Job | receive-job <<<<  
    + CategoryInfo          : OperationStopped: (test:String) [Receive-Job], RuntimeException
    + FullyQualifiedErrorId : test
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:04:51+00:00Added an answer on May 28, 2026 at 12:04 am

    Using throw will change the job object’s State property to “Failed”. The key is to use the job object returned from Start-Job or Get-Job and check the State property. You can then access the exception message from the job object itself.

    Per your request I updated the example to also include concurrency.

    $createZip = {
        Param ( [String] $source, [String] $zipfile )
    
        if ($source -eq "b") {
            throw "Failed to create $zipfile"
        } else {
            return "Successfully created $zipfile"
        }
    }
    
    $jobs = @()
    $sources = "a", "b", "c"
    
    foreach ($source in $sources) {
        $jobs += Start-Job -ScriptBlock $createZip -ArgumentList $source, "${source}.zip"
    }
    
    Wait-Job -Job $jobs | Out-Null
    
    foreach ($job in $jobs) {
        if ($job.State -eq 'Failed') {
            Write-Host ($job.ChildJobs[0].JobStateInfo.Reason.Message) -ForegroundColor Red
        } else {
            Write-Host (Receive-Job $job) -ForegroundColor Green 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code. function createZip { Param ([String]$source, [String]$zipfile) Process { echo
I have the following script <?php echo I am alive<br>; die(I am dying<br>); echo
I have the following script that will update the connection string across three different
I have the following script inside a HTML page: <script> function Test(){ alert(i got
I have the following script: http://jsfiddle.net/oshirowanen/gcYeB/ How do I split the string being displayed
I have the following script cat $1 | while read line do line=`echo $line
I have the following script to clean my Visual Studio 2010 solution directory: @echo
I have the following script: #!/usr/bin/env ruby require 'rubygems' require 'net/ssh' Net::SSH.start('host1', 'root', :password
I have the following script. It replaces all instances of @lookFor with @replaceWith in
I have the following script, where the first and third document.writeline are static and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.