I am looking to limit the percentage of the CPU time used by a PowerShell process to a certain number — for the sake of argument, let’s imagine it to be 13%.
Other options that are not precisely what I need:
1) Setting priority.
2) Setting CPU affinity.
Basically, we have monitoring software which complains if the total CPU usage gets too high. We have a daily process that sets this off — mostly harmlessly, but too many false positives in a monitoring system and people become inured to warnings/errors when we do not wish that.
The process itself gets lsass.exe very excited, too, as it runs, and other processes happen, as well.
I do not know PowerShell and am attempting to fix Somebody Else’s Powershell. Obviously, a ground-up rewrite would be nice at some future point, but for now, bells are ringing and annoying people.
What you’re asking for isn’t really possible. The Windows kernel is in charge of scheduling the CPU — and rightfully so. (I for one don’t want to return to real-mode DOS).
The best you can do is insert a long Sleep() in between each line of the script. But there’s no guarantee that any particular Powershell cmdlet / function / etc will throttle itself the way you want. The Windows API calls that ultimately execute each statement’s dirty work certainly won’t.
Ironically, Raymond touched on this topic just a few days ago: http://blogs.msdn.com/oldnewthing/archive/2009/07/27/9849503.aspx
My real suggestion is to modify your script so it looks like:
# ...the current script contents...