如何在可执行文件崩溃和阻止后继续执行 powershell 脚本
我有这个简单的脚本:
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有几个注意事项:
-ErrorAction
这样的参数也没有任何价值。这仅适用于函数/cmdlet(还有其他吗?)。$lastexitcode
。请注意,正确编码的应用程序应该真正返回其退出代码,并且可能向控制台写入一些内容。如果该消息框严重失败,则没有任何借口。 Main 函数中至少应使用一个大的 try/catch 块。
Several notes apply here:
-ErrorAction
has no value. That applies only to functions/cmdlets (anything else?).$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.
我知道这不是问题的直接答案,就好像应用程序没有报告其错误一样,那么您可能无法在 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/
.\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.