Run script to check CPU and Memory utilization

If you would like to quickly display CPU and Memory utilization in PowerShell console then this script is for you. This is another great example on how to use PowerShell custom objects and add it to array.

CPU and Memory utilization

In past I often had this situation when one of the servers was unresponsive due to high performance which was causing service interruption. Unfortunately SCOM server didn’t catch that CPU or Memory sudden spike and I had to scan all of our servers manually to find what is happening. This is why the following script has been created to avoid similar cases in the future and to quickly identify culprit.

Script is based on Get-WmiObject commands:

# Processor utilization
$Processor = (Get-WmiObject -ComputerName $Server -Class win32_processor -ErrorAction Stop | Measure-Object -Property LoadPercentage -Average | Select-Object Average).Average
 
# Memory utilization
$ComputerMemory = Get-WmiObject -ComputerName $Server -Class win32_operatingsystem -ErrorAction Stop
$Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)
$RoundMemory = [math]::Round($Memory, 2)

Output:

CPU and Memory
CPU and Memory

Final script:

$Servers = Get-Content "C:\users\$env:username\desktop\servers.txt"
$Array = @()
 
ForEach($Server in $Servers)
{
    $Server = $Server.trim()

    Write-Host "Processing $Server"

    $Check = $null
    $Processor = $null
    $ComputerMemory = $null
    $RoundMemory = $null
    $Object = $null

    # Creating custom object
    $Object = New-Object PSCustomObject
    $Object | Add-Member -MemberType NoteProperty -Name "Server name" -Value $Server

    $Check = Test-Path -Path "\\$Server\c$" -ErrorAction SilentlyContinue

    If($Check -match "True")
    {
        $Status = "True"

        Try
        {
            # Processor utilization
            $Processor = (Get-WmiObject -ComputerName $Server -Class win32_processor -ErrorAction Stop | Measure-Object -Property LoadPercentage -Average | Select-Object Average).Average
 
            # Memory utilization
            $ComputerMemory = Get-WmiObject -ComputerName $Server -Class win32_operatingsystem -ErrorAction Stop
            $Memory = ((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)
            $RoundMemory = [math]::Round($Memory, 2)
        }
        Catch
        {
            Write-Host "Something went wrong" -ForegroundColor Red
            Continue
        }

        If(!$Processor -and $RoundMemory)
        {
            $RoundMemory = "(null)"
            $Processor = "(null)"
        }

        $Object | Add-Member -MemberType NoteProperty -Name "Is online?" -Value $Status
        $Object | Add-Member -MemberType NoteProperty -Name "Memory %" -Value $RoundMemory
        $Object | Add-Member -MemberType NoteProperty -Name "CPU %" -Value $Processor

        # Display resutls for single server in realtime
        #$Object
 
        # Adding custom object to our array
        $Array += $Object
    }
    Else
    {
        $Object | Add-Member -MemberType NoteProperty -Name "Is online?" -Value "False"
        $Object | Add-Member -MemberType NoteProperty -Name "Memory %" -Value "(null)"
        $Object | Add-Member -MemberType NoteProperty -Name "CPU %" -Value "(null)"
        
        # Display resutls for single server in realtime
        #$Object
 
        # Adding custom object to our array
        $Array += $Object
    }
}  

    If($Array)
    { 
        $Array | Sort-Object "Is online?" 

        #$Array | Out-GridView
 
        #$Array | Export-Csv -Path "C:\users\$env:username\desktop\results.csv" -NoTypeInformation -Force
    }

Please check also article about getting memory utilization report – link.

2 thoughts on “Run script to check CPU and Memory utilization

  1. Great!!
    Works perfectly fine for the remote servers which are in same domain. How do we get the same for servers in different domain.

    1. Hi Varun,
      You have to add -Credential parameter in Get-WMIObject commands:

      $Servers = Get-Content “C:\users\$env:username\desktop\servers.txt”
      $Array = @()
      $Cred = Get-Credential “domain\$env:username”
      ForEach($Server in $Servers)
      {
      $Server = $Server.trim()

      Write-Host “Processing $Server”

      $Check = $null
      $Processor = $null
      $ComputerMemory = $null
      $RoundMemory = $null
      $Object = $null

      # Creating custom object
      $Object = New-Object PSCustomObject
      $Object | Add-Member -MemberType NoteProperty -Name “Server name” -Value $Server

      $Check = Test-Path -Path “\\$Server\c$” -ErrorAction SilentlyContinue

      If($Check -match “True”)
      {
      $Status = “True”

      Try
      {
      # Processor utilization
      $Processor = (Get-WmiObject -ComputerName $Server -Class win32_processor -ErrorAction Stop -Credential $Cred | Measure-Object -Property LoadPercentage -Average | Select-Object Average).Average

      # Memory utilization
      $ComputerMemory = Get-WmiObject -ComputerName $Server -Class win32_operatingsystem -ErrorAction Stop -Credential $Cred
      $Memory = ((($ComputerMemory.TotalVisibleMemorySize – $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize)
      $RoundMemory = [math]::Round($Memory, 2)
      }
      Catch
      {
      Write-Host “Something went wrong” -ForegroundColor Red
      Continue
      }

      If(!$Processor -and $RoundMemory)
      {
      $RoundMemory = “(null)”
      $Processor = “(null)”
      }

      $Object | Add-Member -MemberType NoteProperty -Name “Is online?” -Value $Status
      $Object | Add-Member -MemberType NoteProperty -Name “Memory %” -Value $RoundMemory
      $Object | Add-Member -MemberType NoteProperty -Name “CPU %” -Value $Processor

      # Display resutls for single server in realtime
      #$Object

      # Adding custom object to our array
      $Array += $Object
      }
      Else
      {
      $Object | Add-Member -MemberType NoteProperty -Name “Is online?” -Value “False”
      $Object | Add-Member -MemberType NoteProperty -Name “Memory %” -Value “(null)”
      $Object | Add-Member -MemberType NoteProperty -Name “CPU %” -Value “(null)”

      # Display resutls for single server in realtime
      #$Object

      # Adding custom object to our array
      $Array += $Object
      }
      }

      If($Array)
      {
      $Array | Sort-Object “Is online?”
      }

Leave a Reply

Your email address will not be published. Required fields are marked *