如何设置 PowerShell 脚本运行的时间限制?
我想对 PowerShell (v2) 脚本设置时间限制,以便在该时间限制到期后强制退出。
我在 PHP 中看到它们有像 set_time_limit 和 max_execution_time 这样的命令,您可以在其中限制脚本甚至函数可以执行的时间。
对于我的脚本,查看时间的 do/while 循环是不合适的,因为我正在调用可能会挂起很长时间的外部代码库。
我想限制一段代码,只允许它运行 x 秒,之后我将终止该代码块并向用户返回脚本超时的响应。
我查看过后台作业,但它们在不同的线程中运行,因此不具有对父线程的终止权限。
有人处理过这个问题或者有解决方案吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
像这样的东西也应该起作用......
Something like this should work too...
这是我的解决方案,灵感来自这篇博文。当所有执行完毕或时间耗尽(以先发生者为准)时,它将完成运行。
我将我想要在有限时间内执行的内容放在一个函数中:
我有另一个函数可以监视计时器以及所有内容是否已完成执行:
>...最后我启动我的函数
WhatIWannaDo
作为后台作业并将其传递给Limit-JobWithTime
(包括如何从作业获取输出的示例):Here's my solution, inspired by this blog post. It will finish running when all has been executed, or time runs out (whichever happens first).
I place the stuff I want to execute during a limited time in a function:
I have another funtion that will keep tabs on a timer and if everything has finished executing:
... and then finally I start my function
WhatIWannaDo
as a background job and pass it on to theLimit-JobWithTime
(including example of how to get output from the Job):我知道这是一篇旧文章,但我在我的脚本中使用了它。
我不确定它的使用是否正确,但是 George 提出的 System.Timers.Timer 给了我一个想法,它似乎对我有用。
我将它用于有时挂起 WMI 查询的服务器,超时会阻止它卡住。
然后,我将消息输出到日志文件,而不是写入主机,这样我就可以看到哪些服务器已损坏,并在需要时修复它们。
我也不使用 guid,而是使用服务器主机名。
我希望这是有道理的并对你有帮助。
I know this is an old post, but I have used this in my scripts.
I am not sure if its the correct use of it, but the System.Timers.Timer that George put up gave me an idea and it seems to be working for me.
I use it for servers that sometimes hang on a WMI query, the timeout stops it getting stuck.
Instead of write-host I then output the message to a log file so I can see which servers are broken and fix them if needed.
I also don't use a guid I use the servers hostname.
I hope this makes sense and helps you.
这是使用计时器的示例。我个人没有尝试过,但我认为它应该有效:
Here is an example of using a Timer. I haven't tried it personally, but I think it should work:
我想出了这个脚本。
代码:
I came up with this script.
Code:
像这样的事情怎么样:
当使用小时或天作为计时器时,请确保调整 $TimeSpan.TotalMinutes
以及 HH:mm 格式,因为这不利于在示例中使用天。
How about something like this:
When using hours or days as a timer, make sure you adjust the $TimeSpan.TotalMinutes
and the HH:mm format, since this does not facilitate the use of days in the example.