We help IT Professionals succeed at work.

PoSh function to get CPU utilization from remote machine

Robert Perez-Corona
Robert Perez-Corona used Ask the Experts™
on
I have a one-liner I need to turn into a function so that I can run against any machine on my AD domain/network. For example, instead of modifying the computername parameter and running the line can I execute something like:

Get-CPU -computername MYREMOTESERVER

Get-WmiObject -computername MYREMOTESERVER win32_processor | Measure-Object -property LoadPercentage -Average | Select Average

Thank you in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroCEO Faru Bonon IT /Top Rated Freelancer on Upwork / Photographer
Awarded 2018
Distinguished Expert 2018

Commented:
function Get-CPU{
    [Cmdletbinding()]
    parameter(
        [parameter(mandatory=$true, position=0)]$remoteserver
    )
    Process{
        $result=Get-WmiObject -computername $remoteserver win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
    }
    end{
        return $result
    }
}

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
We want to stay with the common naming conventions, Jose, and the asker correctly asked for a parameter ComputerName,  so $remoteserver should be replaced by $ComputerName.
Also the process technique is oversized here, and even counterproductive as it will not allow proper pipeline processing of the result.

I think a much simpler approach is much better here
function Get-CPU ([String] $ComputerName = $env:ComputerName)
{
  Get-WmiObject -computername $ComputerName win32_processor | Measure-Object -property LoadPercentage -Average | Select Average
}

Open in new window

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Depends on how many servers at a time should be queried. There's nothing wrong with pipeline support when done correctly.
And I wouldn't call it Get-CPU, because you're not getting the CPU, you're getting its load, so I'd call it Get-CpuLoad.
Anyway, this supports passing computer names using the pipeline, or as an array.
Get-CPULoad -ComputerName 'Server1', 'Server2'
Get-CPULoad -ComputerName (Get-Content .\servers.txt)
'Server1', 'Server2' | Get-CPULoad
Get-Content .\servers.txt | Get-CPULoad

Open in new window

Function Get-CPULoad {
[CmdletBinding()]
Param(
	[Parameter(Position=0, ValueFromPipeline=$true)]
	[String[]]$ComputerName = $env:ComputerName
)
	Process {
		$ComputerName | ForEach-Object {
			$computer = $_
			Get-WmiObject -Class Win32_Processor -ComputerName $_ |
				Measure-Object -Property LoadPercentage -Average |
				Select-Object -Property @{n='ComputerName'; e={$computer}}, @{n='CpuLoad'; e={$_.Average}}
		}
	}
}

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
I agree that is much better  - and contains the respective computername in the output, which is missing from the former suggestions ;-).
Robert Perez-CoronaSystems Administrator

Author

Commented:
thank you all