👊Automating Network Health Checks with PowerShell & Scheduled Tasks⚙️
Learn to automate network checks with PowerShell & Task Scheduler for reliable connectivity & latency monitoring.

Driving SD-WAN Adoption in South Africa
Search for a command to run...
Learn to automate network checks with PowerShell & Task Scheduler for reliable connectivity & latency monitoring.

Driving SD-WAN Adoption in South Africa
No comments yet. Be the first to comment.
Enhancing Security and Compliance with ManageEngine ADAudit Plus

Why Pings Aren't Enough & NMS is Essential

What is SD-WAN (Software Defined Wide Area Networking)? | The Mechanics of this Groundbreaking New Network Technology

Embracing First Principles & the Scientific Method

Ever Wondered | "Is My Internet Really 99.9% Reliable?" 🤔

Monitoring network connectivity is crucial, especially if you're managing infrastructure or working with mission-critical applications. A simple way to ensure your network remains healthy is by periodically checking latency to key destinations and logging the results.
In this guide, I'll show you how to:
Use PowerShell to ping both an external server (1.1.1.1) and your default gateway.
Send the latency results to an external monitoring API.
Automate the script using Windows Task Scheduler so it runs every 10 minutes.
We’ll create a script that:
✅ Dynamically detects the default gateway.
✅ Pings both 1.1.1.1 (Cloudflare DNS) and the gateway.
✅ Sends the latency results to an external API (digger.uptimekuma.com).
✅ Gracefully handles failures (e.g., network down).
heartbeat.ps1)# Get the default gateway reliably
$gateway = (Get-NetIPConfiguration | Where-Object { $_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.Status -eq "Up" }).IPv4DefaultGateway.NextHop
# Function to calculate average ping latency
function Get-AveragePing {
param ($Target)
try {
$pingResults = Test-Connection -ComputerName $Target -Count 5 -ErrorAction Stop
return ($pingResults | Measure-Object -Property ResponseTime -Average).Average
} catch {
return "N/A"
}
}
# Ping 1.1.1.1 and send the result
$latency1 = Get-AveragePing -Target "1.1.1.1"
$api1 = "https://digger.uptimekuma.com/api/push/O6SLd8pNDv?status=up&msg=OK&ping=$latency1"
Invoke-RestMethod -Uri $api1 -Method Get
# Ping the local gateway and send the result
$latency2 = Get-AveragePing -Target $gateway
$api2 = "https://digger.uptimekuma.com/api/push/GwQREfeG3V?status=up&msg=OK&ping=$latency2"
Invoke-RestMethod -Uri $api2 -Method Get
🔹 How it Works:
Get-NetIPConfiguration fetches the correct default gateway dynamically.
The Get-AveragePing function runs 5 pings to the given target and calculates the average response time.
Two API calls send latency results to digger.uptimekuma.com.
If a ping fails, it returns "N/A" instead of crashing.
We’ll now automate this script to run every 10 minutes using Task Scheduler.
Open Notepad, paste the script, and save it as:
C:\Binaries\heartbeat.ps1
By default, Windows restricts PowerShell scripts. To fix this:
Open PowerShell as Administrator
Run:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
1️⃣ Open Task Scheduler (Win + R → taskschd.msc → Enter).
2️⃣ Click "Create Basic Task" on the right.
3️⃣ Name it "Network Heartbeat", then click Next.
4️⃣ Select "Daily", then Next.
5️⃣ Set Start Time (any time) and Recur every 1 days, then Next.
6️⃣ Choose "Repeat task every 10 minutes", for a duration of 1 day.
7️⃣ Select "Start a Program", then Next.
8️⃣ In Program/Script, enter:
powershell.exe
9️⃣ In Add arguments, enter:
-ExecutionPolicy Bypass -File "C:\Binaries\heartbeat.ps1"
🔟 Click Finish.

To test before scheduling:
powershell.exe -ExecutionPolicy Bypass -File "C:\Binaries\heartbeat.ps1"
If everything is working, you should see API requests sent without errors.
🚀 Now, the script will automatically run every 10 minutes and log latency metrics to an external API. This provides continuous monitoring without manual intervention.
🔧 Need More? Modify the script to:
Log results to a file instead.
Send email alerts if latency exceeds a threshold.
Monitor additional network endpoints.
This method is lightweight, effective, and fully automated—ideal for keeping an eye on network health!
