user1202278
user1202278

Reputation: 773

PowerShell detect if command terminated

I have a long running script that runs as a PowerShell command for many hours. Occasionally it terminates - usually OutOfMemory. When this occurs the PowerShell window returns to PS C:\XXXXX>.

I thought simply I could have an additional PS window that listens if the other terminates in an effort to keep it alive. If the other PS window returned to PS C:\XXXX it could start the script again as a back.

Is there a simple way to do this?

Thanks

Upvotes: 2

Views: 233

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175065

Wrap the script invocation in a try/catch block to handle the OOM exception, then wrap that in a loop:

do {
    try {
        # Run the script
        .\script.ps1
        $Success = $true
    }
    catch {
        # Something happened, make sure we try again
        $Success = $false
    }
} while (-not $Success)

Upvotes: 3

Related Questions