简体   繁体   English

通过powershell命令减少线程以减少powershell进程的CPU使用率

[英]Reduce threads to reduce CPU usage of powershell process through powershell command

I have used a Fake cpu usage script in powershell,when i run it CPU usage increases,i can see the thread count.我在 powershell 中使用了一个 Fake cpu 使用脚本,当我运行它时,CPU 使用率会增加,我可以看到线程数。 Now i have to reduce/increase the threadcount inorder reduce the CPU usage.现在我必须减少/增加线程数以减少 CPU 使用率。 Below is the script used for increasing CPU usage.下面是用于增加 CPU 使用率的脚本。 Please help with any powershell command that would reduce/increase thread count for reducing CPU usage.请帮助使用任何可以减少/增加线程数以减少 CPU 使用率的 powershell 命令。 or any other method to reduce CPU usage in this script.或任何其他减少此脚本中 CPU 使用率的方法。 Below is the main part of loop script,by running this in powershell CPU usage increases下面是循环脚本的主要部分,通过在 powershell 中运行它会增加 CPU 使用率

# Code
$num = 2
If ([$num -gt 1])) {
 
 $ThreadCount=(Get-Process -ProcessName Powershell | Select-Object -ExpandProperty Threads).Count
 $ErrorMessage = "Before CPU Saturation threadcount:" + $ThreadCount
 write-AppEventLog $ErrorMessage
 
 $result = 1; 
 foreach ($number in 1..2147483647) {
 $ThreadCount1=(Get-Process -ProcessName Powershell | Select-Object -ExpandProperty Threads).Count
 foreach ($number in 1..2147483647) {
 $result = $result * $number
 $ErrorMessage1 = " CPU Saturation threadcount:" + $ThreadCount1
 write-AppEventLog $ErrorMessage1
 }
}
}

If you want multiple threads, you can use jobs如果你想要多个线程,你可以使用jobs

Start-Job -ScriptBlock { 
# do the thing
}
# Example infinite loop
while($true)
{
    Start-Job -ScriptBlock { 
    # do the thing
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM