-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetwork-SpeedTest.ps1
More file actions
215 lines (175 loc) · 9.77 KB
/
Network-SpeedTest.ps1
File metadata and controls
215 lines (175 loc) · 9.77 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<#
.SYNOPSIS
Test internet speed using Speedtest CLI by Ookla.
.DESCRIPTION
Checks whether the Speedtest CLI is available and runs an internet speed test.
If the required packages are not installed, the script will ask for user permission
before downloading and installing them automatically using Chocolatey.
The script automatically checks for and applies package updates when available.
User confirmation is only requested for complex updates that require uninstalling
and reinstalling packages. Installation and updates require administrator privileges.
.PARAMETER SkipUpdateCheck
Skip checking for package updates and run the speed test with current versions.
.PARAMETER ForceUpdate
Force an update check and prompt even if packages appear to be current.
.NOTES
Author : Mike Brown
Date : 2025-10-09
Version : 2.1
License : MIT
Requires: PowerShell 5.1 or later, Administrator privileges for installation/updates
.EXAMPLE
.\Network-internetSpeed.ps1
Runs the script, installs packages if needed, checks for updates, and performs speed test.
.EXAMPLE
Test-InternetSpeed
Calls the function directly with default behavior (includes update checking).
.EXAMPLE
Test-InternetSpeed -SkipUpdateCheck
Runs the speed test without checking for package updates.
.EXAMPLE
Test-InternetSpeed -ForceUpdate
Forces an update check even if packages appear current.
#>
#########################################################
# Test internet speed using speedtest-cli
#########################################################
function Test-InternetSpeed {
[CmdletBinding()]
param(
[switch]$SkipUpdateCheck,
[switch]$ForceUpdate
)
# Check if running as administrator
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
# Check if speedtest-cli is installed
$speedtestInstalled = Get-Command speedtest -ErrorAction SilentlyContinue
if (-not $speedtestInstalled) {
Write-Host "`nSpeedtest CLI is not installed on this system." -ForegroundColor Yellow
Write-Host "This script needs to install the following packages:" -ForegroundColor Cyan
$packagesToInstall = @()
# Check what needs to be installed
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
$packagesToInstall += "Chocolatey (package manager)"
}
$packagesToInstall += "Speedtest CLI by Ookla"
# Display packages to be installed
foreach ($package in $packagesToInstall) {
Write-Host " - $package" -ForegroundColor White
}
Write-Host "`nThese packages will be downloaded from the internet and installed automatically." -ForegroundColor Yellow
# Ask for user consent
do {
$response = Read-Host "`nDo you want to proceed with the installation? (Y/N)"
$response = $response.Trim().ToUpper()
} while ($response -notin @('Y', 'YES', 'N', 'NO'))
if ($response -in @('N', 'NO')) {
Write-Host "Installation cancelled by user. Speedtest cannot be run without the required packages." -ForegroundColor Red
return
}
# Check for admin privileges
if (-not $isAdmin) {
Write-Error "`nInstallation requires administrator privileges. Please run PowerShell as Administrator and try again."
return
}
Write-Host "`nProceeding with installation..." -ForegroundColor Green
try {
# Install chocolatey if not present
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
Write-Host "Installing Chocolatey package manager..." -ForegroundColor Yellow
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
}
# Install speedtest using chocolatey
Write-Host "Installing Speedtest CLI..." -ForegroundColor Yellow
choco install speedtest -y
# Refresh environment variables
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
Write-Host "Installation completed successfully!" -ForegroundColor Green
}
catch {
Write-Error "Failed to install required packages: $_"
return
}
}
else {
# Packages are installed, check for updates unless skipped
if (-not $SkipUpdateCheck) {
Write-Host "Speedtest CLI is installed. Checking for updates..." -ForegroundColor Green
try {
# Check if chocolatey is available for updates
$chocoInstalled = Get-Command choco -ErrorAction SilentlyContinue
if ($chocoInstalled) {
# Get outdated packages
$outdatedPackages = choco outdated --limit-output --ignore-unfound | Where-Object { $_ -like "*speedtest*" }
if ($outdatedPackages -or $ForceUpdate) {
if ($ForceUpdate) {
Write-Host "Force update requested." -ForegroundColor Yellow
} else {
Write-Host "Speedtest CLI update available!" -ForegroundColor Yellow
}
# Check if we have admin privileges for updating
if (-not $isAdmin) {
Write-Warning "Update requires administrator privileges. Please run PowerShell as Administrator to update packages."
Write-Host "Continuing with current version..." -ForegroundColor Yellow
} else {
Write-Host "Updating Speedtest CLI to the latest version..." -ForegroundColor Green
try {
# Check if this is a complex update that might require user intervention
$updateOutput = choco upgrade speedtest -y --whatif 2>&1
$requiresUninstall = $updateOutput -match "uninstall.*install" -or $updateOutput -match "remove.*add"
if ($requiresUninstall) {
# This update requires uninstall/reinstall - ask user
Write-Host "This update requires uninstalling and reinstalling the package." -ForegroundColor Yellow
do {
$updateResponse = Read-Host "Do you want to proceed with the update? This may temporarily remove the current version (Y/N)"
$updateResponse = $updateResponse.Trim().ToUpper()
} while ($updateResponse -notin @('Y', 'YES', 'N', 'NO'))
if ($updateResponse -in @('N', 'NO')) {
Write-Host "Update cancelled. Continuing with current version..." -ForegroundColor Yellow
return
}
}
# Perform the actual update
Write-Host "Applying update..." -ForegroundColor Green
choco upgrade speedtest -y
Write-Host "Update completed successfully!" -ForegroundColor Green
# Refresh environment variables
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
} catch {
Write-Warning "Update failed: $_"
Write-Host "Continuing with current version..." -ForegroundColor Yellow
}
}
} else {
Write-Host "Speedtest CLI is up to date!" -ForegroundColor Green
}
} else {
Write-Host "Chocolatey not available for update checking. Continuing with current version..." -ForegroundColor Yellow
}
} catch {
Write-Warning "Could not check for updates: $_"
Write-Host "Continuing with current version..." -ForegroundColor Yellow
}
}
}
# Verify speedtest is available after installation
if (-not (Get-Command speedtest -ErrorAction SilentlyContinue)) {
Write-Error "Speedtest CLI is still not available. You may need to restart your PowerShell session."
return
}
# Run the speed test
Write-Host "Running internet speed test..." -ForegroundColor Green
try {
speedtest
Write-Host "Internet speed test completed successfully!" -ForegroundColor Green
}
catch {
Write-Error "Failed to run speed test: $_"
}
}
# Call the function
# Use -SkipUpdateCheck to skip checking for updates
# Use -ForceUpdate to force an update check even if packages appear current
Test-InternetSpeed