Forum Discussion

ashmelburnian's avatar
ashmelburnian
Brass Contributor
Sep 30, 2019

Script to monitor pings & CPU performance

Hi,   Can someone help me build a little script that achieves the following? My programming/PowerShell skills are very limited!    This is to be run on a Windows Server 2012 R2 server. The foll...
  • Kevin_Morgan's avatar
    Sep 30, 2019

    ashmelburnian 

     

    Can you try below script ?

    while (1) {   
       $PingPC = Test-Connection "PIA-W007" -Count 1 -Delay 2 -ErrorAction SilentlyContinue   
       $ResponseTimeStr = if($pingPC -ne $null) { $pingPC.ResponseTime } else {"No Response"}
       $Result = ("{0} - {1}" -f (Get-Date), $ResponseTimeStr)
       # If response time > 4ms OR no response, then check highest CPU processes
       if($pingPC -eq $null -or $pingPC.ResponseTime -gt 4) {
    	# append to text file the processes taking top 5 highest CPU
            $top5CPUsage = ((Get-Process | Sort CPU -Descending | Select -First 5 | Select -expand ProcessName) -join ",")
            $Result =  ("{0} - {1}" -f  $Result,  $top5CPUsage)	
       }
      Write-Progress -Activity "Ping Status: $($Result)" -Status "Pinging"
     # append to text file
      Add-Content -path c:\ping_log.txt $Result
    }

Resources