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! :flushed:

 

This is to be run on a Windows Server 2012 R2 server. The following is what I have so far, built around the Test-Connection command, trying to determine what processes might be affecting ping response times.

 

# Continuous ping of a workstation
while (1) {
   # If response time > 4ms OR no response, then check highest CPU processes
   if($pingPC = Test-Connection PIA-W007 -Count 1) && ResponseTime <= 4ms {
# append to text file TimeStamp and ResponseTime
} else {
# append to text file TimeStamp and ResponseTime
# Get-Process | Sort CPU
# append to text file the processes taking highest CPU
}
}

 

  • 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
    }
  • Kevin_Morgan's avatar
    Kevin_Morgan
    Iron Contributor

    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
    }
    • ashmelburnian's avatar
      ashmelburnian
      Brass Contributor
      Thank you very much!

      This script is running well. I just added "Start-Sleep -Seconds 1" to the end of the while loop.

Resources