Sahana
Sahana

Reputation: 11

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
 }
}
}

Upvotes: 0

Views: 770

Answers (1)

PowerShellGuy
PowerShellGuy

Reputation: 801

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
    }
}

Upvotes: 0

Related Questions