1

I am automating some report generation with Powershell. It is an unpleasant business, collaging together PDFs with Excel interop and third-party DLLs. Due to all the sketchy things I'm trying to marshall together, sometimes my powershell process dies completely. To mitigate this, I'm running it in a supervisor script something like this:

while($moreToDo) {

    try {
        powershell -NonInteractive -NoProfile ./generate-report.ps1
    }
    catch {
        # clean up
        Write-Host "Failed. Trying again..."
    }
}

However, when the powershell process dies, I get a 'windows powershell has stopped working' dialog:

enter image description here

This dialog blocks the supervisor script from continuing, which makes my supervisor script pretty pointless.

Is there a way to invoke powershell so that if it fails catastrophically , it doesn't pop up this message and instead just dies?

Peter
  • 3,619
  • 3
  • 31
  • 37
  • Possible duplicate of https://stackoverflow.com/questions/3561545/how-to-terminate-a-program-when-it-crashes-which-should-just-fail-a-unit-test – Ryan Bemrose Jun 23 '16 at 05:40
  • Are you using a GUI of some sort (Windows Forms or WPF) ? – sodawillow Jun 23 '16 at 08:43
  • @sodawillow My script is just a plain old powershell script. But it's calling out to Excel interop, which means it's firing up GUI Excel processes. – Peter Jun 23 '16 at 23:36

1 Answers1

3

That dialog is put up by the Windows Error Reporting system. It's the OS responding to the powershell.exe process crashing.

It is possible to disable WER or to configure WER to ignore powershell.exe. See https://stackoverflow.com/a/3637710. That's a very dangerous road to travel, however.

A much better idea is to modify your script to not crash PowerShell. If you post a question with code, we may be able to help you figure out what is causing the crash and how to avoid it.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
  • Thanks, good to know about WER. I'm well aware that it is a much better idea to fix my script. However, it's a bodged-together one-off yak-shaving rush-job full of workarounds and hacks. I don't think it's worth trying to chase down the infrequent and quasi-random failures: I will just deal with it dying occasionally and kill the WER messages when I need to. – Peter Jun 23 '16 at 08:28