简体   繁体   中英

Suppress error window on catastrophic powershell process failure

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:

在此输入图像描述

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM