powershell - how to prevent external script from terminating your script with break statement -
i calling external .ps1 file contains break
statement in error conditions. somehow catch scenario, allow externally printed messages show normal, , continue on subsequent statements in script. if external script has throw
, works fine using try
/catch
. trap
in file, cannot stop script terminating.
for answering question, assume source code of external .ps1 file (authored else , pulled in @ run time) cannot changed.
is want possible, or author of script not thinking playing nice when called externally?
edit: providing following example.
in badscript.ps1:
if((get-date).dayofweek -ne "yesterday"){ write-warning "sorry, can run script yesterday." break }
in myscript.ps1:
.\badscript.ps1 write-host "it today."
the results achieve see warning badscript.ps1 , continue on further statements in myscript.ps1. understand why break statement causes "it today." never printed, wanted find way around it, not author of badscript.ps1.
edit: updating title "powershell try/catch not catch break statement" "how prevent external script terminating script break statement". mention of try/catch more 1 failed solution actual question new title better reflects.
i you're coming from. easiest way push script off job, , wait results. can echo results out receive-job after it's done if want.
so considering bad script have above, , script file calling it:
$path = split-path -path $myinvocation.mycommand.definition -parent $start = start-job -scriptblock { . "$using:path\badscript.ps1" } -name "badscript" $wait = wait-job -name "badscript" -timeout 100 receive-job -name "badscript" get-command -name "get-childitem"
this execute bad script in job, wait results, echo results, , continue executing script it's in.
this wrapped in function scripts might need call (just on safe side.
here's output:
warning: sorry, can run script yesterday. commandtype name version source ----------- ---- ------- ------ cmdlet get-childitem 3.1.0.0 microsoft.powershell.management
Comments
Post a Comment