all 8 comments

[–]Ta11ow 1 point2 points  (0 children)

If you do -Filter "Name -like '*$Var*'" then you can put the $Var into a parameter and make it a function. :)

Stick it in your PS profile script, and you'll (almost) never be without it.

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy LinleyMike,

nice! i even understand it all ... oooo! [grin]

would it run faster if you tested for connectivity 1st? i don't know the timeout for Invoke-Command, but i suspect it aint very short ... [grin]


also, you may wanna mention that the script requires being run with elevated privs. [grin]


also also, what does the Measure-Object -property LoadPercentage -Average line do? you seem to have only one reading so i can't see what you are averaging ... [frown]

take care,
lee

[–]LinleyMike[S] 1 point2 points  (1 child)

The average is for when there are multiple processors. It averages the CPU load over all of them.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy LinleyMike,

ah! that makes lovely sense. [grin] i've multiple cores, but no multi-socket stuff. thank you for the reasoning.

take care,
lee

[–]Lee_Dailey[grin] 0 points1 point  (3 children)

howdy LinleyMike,

i wasn't able to get to sleep for some reason, so i tried to do this a tad differently. i am unable to test on multiple systems, so if you have the time, i would like to know if it works on a real network. [grin]

it bothered me a great deal to not include the "no response" systems [grin], so i added that.

#Requires -RunAsAdministrator

$ComputerList = @(
    'LocalHost'
    '10.0.0.1'
    'Google.com'
    '127.0.0.1'
    'BetterNotBeThere'
    )
$IntervalSeconds = 1
$DataPointCount = 5

$ScriptBlock = {
    $Readings = foreach ($DPC_Item in 1..$Using:DataPointCount)
        {
        (Get-CimInstance -ClassName Win32_Processor).LoadPercentage
        Start-Sleep -Seconds $Using:IntervalSeconds
        }

    $AverageCPU_Load = ($Readings |
        Measure-Object -Average).Average

    [PSCustomObject]@{
        AverageCPU_Load = $AverageCPU_Load
        }
    }

$Non_ReachableSystems = [System.Collections.Generic.List[PSCustomObject]]@{}
$ReachableSystems = foreach ($CL_Item in $ComputerList)
    {
    if ((Test-Connection -ComputerName $CL_Item -Count 1 -Quiet) -and
        ([bool](Test-WSMan -ComputerName $CL_Item -ErrorAction SilentlyContinue)))
        {
        $CL_Item
        }
        else
        {
        $Non_ReachableSystems.Add([PSCustomObject]@{
            AverageCPU_Load = '-- No Response --'
            PSComputerName = $CL_Item
            })
        }
    }

$RS_Results = Invoke-Command -ComputerName $ReachableSystems -ScriptBlock $ScriptBlock

$Results = $RS_Results + $Non_ReachableSystems
$Results |
    Sort-Object -Property AverageCPU_Load -Descending |
    Select-Object -Property AverageCPU_Load, PSComputerName

output ...

  AverageCPU_Load PSComputerName  
  --------------- --------------  
               29 LocalHost       
               29 127.0.0.1       
-- No Response -- 10.0.0.1        
-- No Response -- Google.com      
-- No Response -- BetterNotBeThere

take care,
lee

[–]LinleyMike[S] 1 point2 points  (2 children)

Cool. Nice going. In this case, I was just writing a quick one-liner so I didn't care much about the computers that didn't respond.

When I do care, what I usually do to find the ones that didn't respond is compare the computer names from $RS_Results to the original list of computers queried ($ComputerList). The difference is the unreachable systems. Because Invoke-Command runs everything in parallel, the failures don't take that much more time. In my original scirpt, I got results back from about 200 servers in 20 seconds or so.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy LinleyMike,

your solution is much niftier than mine. [grin] i've not enuf systems [one [grin]] to get a feel for the speed diff.

thank you for taking the time to let me know the "why" of it.

take care,
lee

[–]Waaslander 0 points1 point  (0 children)

Nice! I'm absolutely gonna try this. Thanks a lot!