Reputation: 358
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?
Upvotes: 3
Views: 2705
Reputation: 21
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/
Upvotes: 2
Reputation: 29479
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.
Upvotes: 2
Reputation: 126932
.\MyApp.exe -ErrorAction Stop $file
On a side note, -ErrorAction has no meaning in legacy applications. It's a cmdlet parameter.
Upvotes: 1