2

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

5
  • 4
    for() { .\Script.ps1 } Commented Jul 2, 2017 at 11:14
  • Can you elaborate for me? if within this for loop it will restart if it fails? Commented Jul 2, 2017 at 13:57
  • It's just an infinite loop. Commented Jul 2, 2017 at 14:30
  • @PetSerAl if the script fails continiously powershell will close this infinite loop after some number of executions, Commented Jul 3, 2017 at 1:39
  • @PetSerAl and this doesn't take care if the script is successful,,, no matter what that script is supposed to run forever Commented Jul 3, 2017 at 1:41

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.