如何防止外部脚本使用 break 语句终止脚本 [英] how to prevent external script from terminating your script with break statement

查看:83
本文介绍了如何防止外部脚本使用 break 语句终止脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用一个外部 .ps1 文件,该文件包含在某些错误条件下的 break 语句.我想以某种方式捕捉这种情况,允许任何外部打印的消息正常显示,并继续我的脚本中的后续语句.如果外部脚本有 throw,使用 try/catch 可以很好地工作.即使我的文件中有 trap,我也无法阻止我的脚本终止.

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.

为了回答这个问题,假设外部 .ps1 文件(由其他人编写并在运行时引入)的源代码无法更改.

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?

提供以下示例.

在 badscript.ps1 中:

In badscript.ps1:

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

在 myscript.ps1 中:

In myscript.ps1:

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

我想要达到的结果是看到来自 badscript.ps1 的警告,并让它继续我在 myscript.ps1 中的进一步声明.我理解为什么 break 语句会导致It is today".永远不会被打印,但是我想找到一种解决方法,因为我不是 badscript.ps1 的作者.

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.

将标题从powershell tr​​y/catch 不捕获 break 语句"更新为如何防止外部脚本使用 break 语句终止脚本".提到 try/catch 实际上更多的是针对实际问题的一个失败解决方案,新标题更好地反映了这一点.

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.

推荐答案

我知道你来自哪里.可能最简单的方法是将脚本作为作业推送,然后等待结果.如果您愿意,您甚至可以在完成后使用 Receive-Job 回显结果.

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.

输出如下:

WARNING: Sorry, you can only run this script yesterday.

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Cmdlet          Get-ChildItem                                      3.1.0.0    Microsoft.PowerShell.Management

这篇关于如何防止外部脚本使用 break 语句终止脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆