-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTest-NetworkSpeed_Summary.ps1
More file actions
72 lines (64 loc) · 2.71 KB
/
Test-NetworkSpeed_Summary.ps1
File metadata and controls
72 lines (64 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Axel Christian Lenz
# https://www.linkedin.com/in/axellenz/
# Read the computer list with names from the file
$basePath = "C:\TEMP"
# Here in every line an IP or a HOST name
$computers = Get-Content -Path (Join-Path $basePath "computers.txt")
# Set the intervals for the main loop and summary loop
$interval = 1 * 60 * 1000 # 1 minute in milliseconds
$summaryInterval = 3 * 60 * 1000 # 10 minutes in milliseconds
function Test-NetworkSpeed {
param ($Computer, $Name)
$result = Test-Connection -ComputerName $Computer -Count 1 -ErrorAction SilentlyContinue
@{
Computer = $Computer
Name = $Name
Speed = if ($result) { $result.ResponseTime } else { 0 }
Status = if ($result) { "Online" } else { "Offline" }
Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
}
}
function Write-Summary {
$allResults = Import-Csv -Path (Join-Path $basePath "results.txt")
$summaryData = $computers | ForEach-Object {
$computerData = $_.Split(',')
$computer = $computerData[0]
$name = $computerData[1]
$onlineResults = $allResults | Where-Object {$_.Computer -eq $computer -and $_.Status -eq "Online"}
if ($onlineResults) {
$best = ($onlineResults | Measure-Object -Property Speed_ms -Minimum).Minimum
$worst = ($onlineResults | Measure-Object -Property Speed_ms -Maximum).Maximum
$average = ($onlineResults | Measure-Object -Property Speed_ms -Average).Average
} else {
$best = $null
$worst = $null
$average = $null
}
[PSCustomObject]@{
Computer = $computer
Name = $name
Best = $best
Worst = $worst
Average = $average
Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
}
}
$summaryData | Export-Csv -Path (Join-Path $basePath "summary.txt") -NoTypeInformation -Append
}
$header = "Computer,Name,Speed_ms,Status,Timestamp"
if (-not (Get-Content -Path (Join-Path $basePath "results.txt") -First 1) -eq $header) {
Add-Content -Path (Join-Path $basePath "results.txt") -Value $header
}
$lastSummaryTime = Get-Date
while ($true) {
foreach ($computerLine in $computers) {
$computerData = $computerLine.Split(',')
$testResult = Test-NetworkSpeed -Computer $computerData[0] -Name $computerData[1]
Add-Content -Path (Join-Path $basePath "results.txt") -Value "$($testResult.Computer),$($testResult.Name),$($testResult.Speed),$($testResult.Status),$($testResult.Timestamp)"
}
if (((Get-Date) - $lastSummaryTime).TotalMilliseconds -ge $summaryInterval) {
Write-Summary
$lastSummaryTime = Get-Date
}
Start-Sleep -Milliseconds $interval
}