Powershell: CPU Load Generator

Found this snippet online.
foreach ($loopnumber in 1..2147483647) {$result=1;foreach ($number in 1..2147483647) {$result = $result * $number};$result}

Works great, but only seems to hit a single CPU.  In order for it fully load all CPU's, I decided to augment it by simply having it launch a powershell window per CPU/Core found w/ the same script running in a loop.

Here is what I came up w/ below:
#$NumberofProcs = (Get-WMIObject win32_processor | Measure-Object NumberofLogicalProcessors -sum).sum
#Updated based on anonymous feedback.
$NumberofProcs= [int]$env:Number_of_Processors
While ($NumberofProcs -ne 0) 
{
$NumberofProcs--
Start Powershell.exe -ArgumentList '"foreach ($loopnumber in 1..2147483647) {$result=1;foreach ($number in 1..2147483647) {$result = $result * $number};$result}"'
}
For some context, I was using this script to force vRealize Operations to send a notification for anomalous behavior on a typically low use VM. Thanks anonymous! I'd credit you if I knew who you were.

1 comment:

Anonymous said...

$NumberofProcs = [int]$env:NUMBER_OF_PROCESSORS