SOLVED

Script to monitor pings & CPU performance

Brass Contributor

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
}
}

 

2 Replies
best response confirmed by ashmelburnian (Brass Contributor)
Solution

@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
}
Thank you very much!

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

Accepted Solutions
best response confirmed by ashmelburnian (Brass Contributor)
Solution

@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
}

View solution in original post