简体   繁体   中英

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. Now i have to reduce/increase the threadcount inorder reduce the CPU usage. Below is the script used for increasing CPU usage. Please help with any powershell command that would reduce/increase thread count for reducing CPU usage. or any other method to reduce CPU usage in this script. Below is the main part of loop script,by running this in powershell CPU usage increases

# 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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