Forum Discussion
ashmelburnian
Sep 30, 2019Brass Contributor
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...
- Sep 30, 2019
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
Sep 30, 2019Iron Contributor
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 }
- ashmelburnianOct 02, 2019Brass ContributorThank you very much!
This script is running well. I just added "Start-Sleep -Seconds 1" to the end of the while loop.