How can you set a time limit for a PowerShell script to run for?

Posted by calrain on Stack Overflow See other posts from Stack Overflow or by calrain
Published on 2010-06-11T03:07:36Z Indexed on 2010/06/11 3:13 UTC
Read the original article Hit count: 247

I want to set a time limit on a PowerShell (v2) script so it forcibly exits after that time limit has expired.

I see in PHP they have commands like set_time_limit and max_execution_time where you can limit how long the script and even a function can execute for.

With my script, a do/while loop that is looking at the time isn't appropriate as I am calling an external code library that can just hang for a long time.

I want to limit a block of code and only allow it to run for x seconds, after which I will terminate that code block and return a response to the user that the script timed out.

I have looked at background jobs but they operate in a different thread so won't have kill rights over the parent thread.

Has anyone dealt with this or have a solution?

Thanks!

© Stack Overflow or respective owner

Related posts about powershell

Related posts about time