-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeedtestCLI-PS.ps1
More file actions
157 lines (138 loc) · 5.38 KB
/
SpeedtestCLI-PS.ps1
File metadata and controls
157 lines (138 loc) · 5.38 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# --------------------------------------------
# CONFIGURATION
# --------------------------------------------
# Set to "true" to write the log file, or "false" to skip logging
$SaveLog = "true"
# Set to "true" to save the raw JSON, or "false" to skip saving JSON
$SaveJson = "true"
# Set to "true" to delete the ZIP, EXE and MD files when done; "false" to leave them
$EnableCleanup = "true"
# How many times to retry on failure
$MaxRetries = 3
# Delay between retries (seconds)
$RetryDelaySeconds = 5
# Ookla Speedtest CLI download URL (win64)
$Url = "https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-win64.zip"
# Where to store everything
$TargetFolder = "C:\temp"
$ExtractFolder = Join-Path $TargetFolder "speedtest"
$TempZip = Join-Path $TargetFolder "speedtest.zip"
$ExePath = Join-Path $ExtractFolder "speedtest.exe"
# --------------------------------------------
# PREP: ensure folders exist
# --------------------------------------------
if (-not (Test-Path $ExtractFolder)) {
New-Item -Path $ExtractFolder -ItemType Directory -Force | Out-Null
}
# --------------------------------------------
# DOWNLOAD & EXTRACT (once)
# --------------------------------------------
if (-not (Test-Path $ExePath)) {
Write-Host "Downloading Speedtest CLI to $TempZip..."
Invoke-WebRequest -Uri $Url -OutFile $TempZip -UseBasicParsing
Write-Host "Extracting to $ExtractFolder..."
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory($TempZip, $ExtractFolder)
}
# --------------------------------------------
# RUN SPEEDTEST WITH RETRIES & PARSE OUTPUT
# --------------------------------------------
$attempt = 0
do {
$attempt++
Write-Host "Running Speedtest (attempt $attempt of $MaxRetries)…"
# capture stdout+stderr as objects
$rawObjects = & $ExePath --accept-license --accept-gdpr --format json 2>&1
# convert everything to string
$rawLines = $rawObjects | ForEach-Object { $_.ToString() }
$rawText = $rawLines -join "`n"
# locate JSON object within the output
$startIndex = $rawText.IndexOf('{')
$endIndex = $rawText.LastIndexOf('}')
if ($startIndex -lt 0 -or $endIndex -lt $startIndex) {
Write-Warning " → no JSON payload detected."
if ($attempt -lt $MaxRetries) { Start-Sleep -Seconds $RetryDelaySeconds; continue }
else { Write-Error "All $MaxRetries attempts failed: no JSON returned."; exit 1 }
}
# extract JSON substring
$json = $rawText.Substring($startIndex, $endIndex - $startIndex + 1)
# try parse
try {
$data = $json | ConvertFrom-Json
} catch {
Write-Warning " → failed to parse JSON."
if ($attempt -lt $MaxRetries) { Start-Sleep -Seconds $RetryDelaySeconds; continue }
else { Write-Error "All $MaxRetries attempts failed parsing JSON."; exit 1 }
}
# check for speedtest error
if ($null -ne $data.error) {
Write-Warning " → Speedtest returned error: $($data.error)"
if ($attempt -lt $MaxRetries) { Start-Sleep -Seconds $RetryDelaySeconds; continue }
else { Write-Error "All $MaxRetries attempts failed: $($data.error)"; exit 1 }
}
# success!
break
} while ($attempt -lt $MaxRetries)
# --------------------------------------------
# EXTRACT METRICS
# --------------------------------------------
$Latency = [math]::Round($data.ping.latency, 2)
$Download = [math]::Round(($data.download.bandwidth * 8) / 1MB, 2)
$Upload = [math]::Round(($data.upload.bandwidth * 8) / 1MB, 2)
$ResultURL = $data.result.url
# --------------------------------------------
# SAVE RAW JSON (if enabled)
# --------------------------------------------
$Date = Get-Date -Format 'yyyy-MM-dd'
if ($SaveJson -eq "true") {
$JsonFile = Join-Path $ExtractFolder "$Date-Speedtest.json"
$json | Out-File -FilePath $JsonFile -Encoding UTF8
Write-Host "`nRaw JSON saved to: $JsonFile"
}
# --------------------------------------------
# LOGGING (if enabled)
# --------------------------------------------
if ($SaveLog -eq "true") {
$LogFile = Join-Path $ExtractFolder "$Date-Speedtest.txt"
$LogContent = @(
"Timestamp : $(Get-Date -Format 'u')"
""
"=== Full JSON Results ==="
$json
""
"=== Summary ==="
"Latency : $Latency ms"
"Download : $Download Mbps"
"Upload : $Upload Mbps"
""
"Result URL : $ResultURL"
)
$LogContent | Out-File -FilePath $LogFile -Encoding UTF8
Write-Host "`nResults have been logged to: $LogFile"
}
# --------------------------------------------
# CONSOLE OUTPUT
# --------------------------------------------
Write-Host ""
Write-Host "Latency : $Latency ms"
Write-Host "Download : $Download Mbps"
Write-Host "Upload : $Upload Mbps"
Write-Host "Result URL : $ResultURL"
# --------------------------------------------
# CLEANUP (if enabled)
# --------------------------------------------
if ($EnableCleanup -eq "true") {
if (Test-Path $TempZip) {
Remove-Item -Path $TempZip -Force
Write-Host "Deleted: $TempZip"
}
if (Test-Path $ExePath) {
Remove-Item -Path $ExePath -Force
Write-Host "Deleted: $ExePath"
}
$MdFile = Join-Path $ExtractFolder "speedtest.md"
if (Test-Path $MdFile) {
Remove-Item -Path $MdFile -Force
Write-Host "Deleted: $MdFile"
}
}