# Need Parent script to capture error in child script and stop execution

**URL:** <https://forums.powershell.org/t/need-parent-script-to-capture-error-in-child-script-and-stop-execution/22806>\
**Category:** PowerShell Help\
**Created:** [August 23, 2023, 4:20pm UTC](https://forums.powershell.org/t/need-parent-script-to-capture-error-in-child-script-and-stop-execution/22806 "2023-08-23T16:20:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lrobb](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@Lrobb](https://forums.powershell.org/u/Lrobb)\
**Post date:** [August 23, 2023, 4:20pm UTC](https://forums.powershell.org/t/need-parent-script-to-capture-error-in-child-script-and-stop-execution/22806/1 "2023-08-23T16:20:53Z")

</div>

Hey all,

As the title states, I need some help with exception/error handling between parent/child scripts. I am calling a child script using Invoke-Psake, and if the child script encounters and error I would like the parent script to capture the error and then have both the parent/child stop execution unless specified to continue. Below is my code, and any help would be appreciated!

Parent Script:

```auto
Install-Module PSake
Import-Module Psake

try {
    invoke-psake -taskList @("ChildTask") -buildFile ".\childscript1.ps1"
    Write-Host "Parent script executed successfully"
} catch {
    Write-Host "An error occurred in the parent script:"
    write-Host $_
    exit 1
}

write-host "still going"

```

Child Script:

```auto
try {
    $errorcondition = $true
    if ($errorcondition) {
        Write-Host "Child script error: an error occurred"
        throw "an error occurred in childscript1.ps1"
    }
} catch {
    Write-Host "child script caught an exception : $_"
    throw $_
}

```

Console Output:

Child script error: an error occurred  
child script caught an exception : an error occurred in childscript1.ps1  
Error: 8/23/2023 12:10:01 PM:  
At C:\Users\LRobbins\Desktop\powershell\childscript1.ps1:5 char:9 + throw “an error occurred in childscript1.ps1” + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [\<\<==\>\>] Exception: an error occurred in childscript1.ps1  
Parent script executed successfully  
still going

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [August 23, 2023, 8:39pm UTC](https://forums.powershell.org/t/need-parent-script-to-capture-error-in-child-script-and-stop-execution/22806/2 "2023-08-23T20:39:46Z")

</div>

Have you tried adding `-ErrorAction Stop` to `Invoke-PSake`?

```auto
try {
    Invoke-Psake -taskList @("ChildTask") -buildFile ".\childscript1.ps1" -ErrorAction Stop
    Write-Host "Parent script executed successfully"
} catch {
    Write-Host "An error occurred in the parent script:"
    Write-Host $_
    exit 1
}

```

---

<div class="post-metadata">

**Author:** ![Lrobb](https://avatars.discourse-cdn.com/v4/letter/l/c2a13f/32.png) [@Lrobb](https://forums.powershell.org/u/Lrobb)\
**Post date:** [August 23, 2023, 8:40pm UTC](https://forums.powershell.org/t/need-parent-script-to-capture-error-in-child-script-and-stop-execution/22806/3 "2023-08-23T20:40:30Z")

</div>

Yes, I have also tried this in my testing.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/need-parent-script-to-capture-error-in-child-script-and-stop-execution/22806/4 "2024-05-16T20:14:43Z")

</div>

Closed on May 16, 2024, 8:14pm UTC


