如何在可执行文件崩溃和阻止后继续执行 powershell 脚本

发布于 2024-09-10 13:44:32 字数 458 浏览 4 评论 0原文

我有这个简单的脚本:

$files = dir .\configs | ? { !$_.PSIsContainer }

foreach($file in $files)
{
    try
    {
        .\MyApp.exe -ErrorAction Stop $file
    }
    catch
    {
         write-host "!!!!!!!!!!!!error!!!!!!!!!!!!!!"
         continue
    }   
}

问题是当

.\MyApp.exe -ErrorAction Stop $file

崩溃时,出现有关应用程序崩溃的 Windows 消息框,并且我的脚本块、catch 未命中,继续的唯一方法是单击消息框中的 Storno 按钮。

那么如何防止阻塞呢?

I have this simple script:

$files = dir .\configs | ? { !$_.PSIsContainer }

foreach($file in $files)
{
    try
    {
        .\MyApp.exe -ErrorAction Stop $file
    }
    catch
    {
         write-host "!!!!!!!!!!!!error!!!!!!!!!!!!!!"
         continue
    }   
}

The problem is that when

.\MyApp.exe -ErrorAction Stop $file

crash, the windows message box about application crash appear and my script block, catch is not hit and only way to continue is click Storno button in the message box.

So how to prevent blocking?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏剧牡丹亭 2024-09-17 13:44:32

这里有几个注意事项:

  • 该对话框由操作系统管理,因此与 PowerShell 无关。 PowerShell 对此无能为力。您可以使用一些自动化功能来查找窗口并“单击”按钮,但这确实很尴尬。
  • 同样应用像 -ErrorAction 这样的参数也没有任何价值。这仅适用于函数/cmdlet(还有其他吗?)。
  • 应用程序应该返回 0(成功)或其他任何值(失败),它们不会抛出异常。在您的情况下,您可以使用包含应用程序退出代码的 $lastexitcode

请注意,正确编码的应用程序应该真正返回其退出代码,并且可能向控制台写入一些内容。如果该消息框严重失败,则没有任何借口。 Main 函数中至少应使用一个大的 try/catch 块。

Several notes apply here:

  • This dialog is managed by operating system, so there is nothing about PowerShell. PowerShell can't do anything about it. You could use some automation to find a window and 'click' button, but that's really awkward.
  • Also applying parameters like -ErrorAction has no value. That applies only to functions/cmdlets (anything else?).
  • Applications are supposed to return 0 (success) or anything else (failure), they don't throw exceptions. In your case you can use $lastexitcode that contains the exit code of the application.

Note, that properly coded application should really return its exit code and may write something to console. If it fails horribly with that message box, there is no excuse. At least one big try/catch block in the Main function should be used.

柠檬色的秋千 2024-09-17 13:44:32

我知道这不是问题的直接答案,就好像应用程序没有报告其错误一样,那么您可能无法在 powershell 内捕获所述错误。

但是,如果问题是该对话框导致您的脚本停止,并且您希望它继续运行,您可以禁止在窗口中弹出“MyApp.exe 已停止工作”错误对话框,这将允许您的脚本继续。

请参阅此博客了解更多信息:
https:// /www.raymond.cc/blog/disable-program-has-stopped-working-error-dialog-in-windows-server-2008/

I know this isn't a direct answer to the question, as if the application does not report its error, then it is possible that you might not be able to capture said error inside of powershell.

However, if the issue is that the dialog box is causing your script to halt, and you want it to continue anyway, you can disable the "MyApp.exe Has Stopped Working" error dialog box from popping up in windows which will allow your script to continue.

See this blog for more information:
https://www.raymond.cc/blog/disable-program-has-stopped-working-error-dialog-in-windows-server-2008/

殤城〤 2024-09-17 13:44:32

.\MyApp.exe -ErrorAction Stop $file

附带说明,-ErrorAction 在旧应用程序中没有任何意义。这是一个 cmdlet 参数。

.\MyApp.exe -ErrorAction Stop $file

On a side note, -ErrorAction has no meaning in legacy applications. It's a cmdlet parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文