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:
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:
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