Skip to content

Commit deca59a

Browse files
authored
Add new script 📜
1 parent 8084f43 commit deca59a

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<#
2+
.SYNOPSIS
3+
Get-LatencyReport
4+
5+
.DESCRIPTION
6+
This script pings a list of servers to check network latency. It outputs the results as a custom PowerShell object, including each server's response status and average response time in milliseconds. This can be used to monitor the network connectivity and performance between the host machine and specified servers.
7+
8+
.PARAMETER Servers
9+
A list of server DNS names or IP addresses to ping.
10+
11+
.PARAMETER PingCount
12+
How many pings to the remote computer in order to gain the average.
13+
14+
.EXAMPLE
15+
PS C:\> .\Get-LatencyReport.ps1
16+
17+
This example uses the predefined list of servers in the script and outputs their latency report.
18+
19+
.NOTES
20+
Modify the `$servers` variable to include the servers you wish to test. The script currently pings each server four times to calculate the average latency.
21+
22+
Date Created: April 4th, 2024
23+
24+
.AUTHOR
25+
Blake Drumm (blakedrumm@microsoft.com)
26+
#>
27+
[CmdletBinding()]
28+
param
29+
(
30+
[string[]]$Servers,
31+
[int]$PingCount = 4
32+
)
33+
34+
if (-NOT $Servers)
35+
{
36+
# Define a list of servers to ping
37+
$Servers = @"
38+
google.com
39+
microsoft.com
40+
server3.contoso.com
41+
"@ -split [Environment]::NewLine
42+
}
43+
44+
# Loop through each server in the list
45+
foreach ($server in $Servers)
46+
{
47+
# Skip empty lines
48+
if (-not [string]::IsNullOrWhiteSpace($server))
49+
{
50+
try
51+
{
52+
# Ping the server with 4 echo requests
53+
$pingResults = Test-Connection -ComputerName $server -Count $PingCount -ErrorAction Stop
54+
55+
# Calculate the average response time
56+
$averageResponseTime = ($pingResults | Measure-Object ResponseTime -Average).Average
57+
58+
# Output the result as a custom object
59+
[PSCustomObject]@{
60+
Server = $server
61+
Status = "Success"
62+
AverageResponseTime = "$averageResponseTime ms"
63+
}
64+
}
65+
catch
66+
{
67+
# If the ping fails, output the server with a failure status and no response time
68+
[PSCustomObject]@{
69+
Server = $server
70+
Status = "Failed"
71+
AverageResponseTime = "N/A"
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)