Search This Blog

Monday, October 8, 2018

Powershell One Liners: Get current cpu utilization on all your Servers

Powershell One Liner to Get current cpu utilization on all Servers

There are couple pre-requisites to get the CPU usage on servers.

  • You need to have the administrative rights on the servers
  • PowerShell must be launched in elevated privileges mode


# Assuming you have list of servers in servers.txt, each server name in its own line

$ComputerNames = get-content servers.txt


# Method 1 - Using the Get-Counter cmdlet
# Notice that here you can also easily add the interval and max sample sizes, with the WMI  method shown next, you cannot


Get-Counter -Counter "\Processor(_Total)\% Processor Time" `
            -SampleInterval 1 `
            -MaxSamples 2 `
            -ComputerName $ComputerNames | 
          
            Export-Counter -path PercentageProcessorTime.csv `
                           -fileformat csv -force


# Method 2 - Using WMI Object

Get-WmiObject -Query "Select * from Win32_PerfFormattedData_PerfOS_Processor where name = '_Total'" `
              -ComputerName $ComputerNames | 
              sort PercentProcessorTime -descending | 
              ft -Property PSComputerName, name, PercentProcessorTime -autosize