how to prevent external script from terminating your script with break statement

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I am calling an external .ps1 file which contains a break statement in certain error conditions. I would like to somehow catch this scenario, allow any externally printed messages to show as normal, and continue on with subsequent statements in my script. If the external script has a throw, this works fine using try/catch. Even with trap in my file, I cannot stop my script from terminating.

For answering this question, assume that the source code of the external .ps1 file (authored by someone else and pulled in at run time) cannot be changed.

Is what I want possible, or was the author of the script just not thinking about playing nice when called externally?

Edit: providing the following example.

In badscript.ps1:

if((Get-Date).DayOfWeek -ne "Yesterday"){     Write-Warning "Sorry, you can only run this script yesterday."     break }

In myscript.ps1:

.\badscript.ps1 Write-Host "It is today."

The results I would like to achieve is to see the warning from badscript.ps1 and for it to continue on with my further statements in myscript.ps1. I understand why the break statement causes "It is today." to never be printed, however I wanted to find a way around it, as I am not the author of badscript.ps1.

Edit: Updating title from "powershell try/catch does not catch a break statement" to "how to prevent external script from terminating your script with break statement". The mention of try/catch was really more about one failed solution to the actual question which the new title better reflects.

回答1:

I get where you're coming from. Probably the easiest way would be to push the script off as a job, and wait for the results. You can even echo the results out with Receive-Job after it's done if you want.

So considering the bad script you have above, and this 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 will execute the bad script in a job, wait for the results, echo the results, and then continue executing the script it's in.

This could be wrapped in a function for any scripts you might need to call (just to be on the safe side.

Here's the output:

WARNING: Sorry, you can only run this script yesterday.  CommandType     Name                                               Version    Source -----------     ----                                               -------    ------ Cmdlet          Get-ChildItem                                      3.1.0.0    Microsoft.PowerShell.Management


回答2:

Running a separate PowerShell process from within my script to invoke the external file has ended up being a solution good enough for my needs: powershell -File .\badscript.ps1 will execute the contents of badscript.ps1 up until the break statement including any Write-Host or Write-Warning's and let my own script continue afterwards.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!