In Windows PowerShell, how to set the maximum% processor for Script to use?

I want to limit the percentage of CPU time used by the PowerShell process to a specific number β€” for the sake of argument, suppose it is 13%.

Other options that are not quite what I need: 1) Priority setting. 2) Setting the proximity of the processor.

Basically, we have monitoring software that complains if overall CPU usage gets too high. We have a daily process that delays this action - basically harmless, but there are too many false positives in the monitoring system, and people become dependent on warnings / errors when we do not want to.

The process itself gets lsass.exe also very excited as it works, and other processes also occur.

I do not know PowerShell and am trying to fix Somebody Else Powershell. Obviously, zero-based tuning would be good in the future, but for now, bells ring and annoy people.

+3
source share
5 answers

What you ask for is actually impossible. The Windows kernel is responsible for scheduling the processor - and rightly so. (For one, I do not want to return to DOS real mode).

, , () script. , - / Powershell/etc , . Windows API, , , , .

, : http://blogs.msdn.com/oldnewthing/archive/2009/07/27/9849503.aspx

- script, :

try {
    Stop-CpuMonitor

    # ...the current script contents...
}
finally {
    Start-CpuMonitor
}                
+3

, , Powershell . , Powershell Low, / . , ( , 15 , ) -, , , . , - , .

/

+1

, , . - , - .

ThreadPriority , PowerShell , , , - . powershell :

Get-Process -name powershell | foreach { $_.PriorityClass = "Idle" }

. , powershell , . , , script .

, - , . 50-100 - .

[System.Threading.Thread]::Sleep(50)

script , , .

+1

, - , , , , PowerShell. script .

, "" "" . , 27%, , . . , , 2%.

, .

script:

function Spin
{
    param($iterations)

    for($i = 0; $i -lt $iterations; ++$i)
    {
        # Do nothing
    }
}

$thread = [System.Threading.Thread]::CurrentThread

Write-Host $thread.Priority
$start = [DateTime]::Now
Write-Host "[$start] Start"

Spin 10000000

$end = [DateTime]::Now
Write-Host "[$end] End"
$span = $end - $start
Write-Host "Time: $span"

$thread.Priority = "Lowest"
Write-Host $thread.Priority
$start = [DateTime]::Now
Write-Host "[$start] Start"

Spin 10000000

$end = [DateTime]::Now
Write-Host "[$end] End"
$span = $end - $start
Write-Host "Time: $span"

$thread.Priority = "Normal"

:

Normal
[08/06/2009 08:12:38] Start
[08/06/2009 08:12:55] End
Time: 00:00:16.7760000
Lowest
[08/06/2009 08:12:55] Start
[08/06/2009 08:13:11] End
Time: 00:00:16.8570000

, Thread.Priority :

.

0

process lasso. , , .

, , , .

PS: . , .

-1

Source: https://habr.com/ru/post/1714513/


All Articles