Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Win_Speedtest.ps1 #239

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 44 additions & 14 deletions scripts/Win_Speedtest.ps1
Original file line number Diff line number Diff line change
@@ -1,27 +1,59 @@

## Measures the speed of the download, can only be ran on a PC running Windows 10 or a server running Server 2016+, plan is to add uploading also
## Majority of this script has been copied/butchered from https://www.ramblingtechie.co.uk/2020/07/13/internet-speed-test-in-powershell/
# MINIMUM ACCEPTED THRESHOLD IN mbps
$mindownloadspeed = 20
$minuploadspeed = 4

# File to download you can find download links for other files here https://speedtest.flonix.net
$downloadurl = "https://files.xlawgaming.com/10mb.bin"
#$UploadURL = "http://ipv4.download.thinkbroadband.com/10MB.zip"
$downloadurls = @(
"https://raw.githubusercontent.com/jamesward/play-load-tests/master/public/10mb.txt"
)

# SIZE OF SPECIFIED FILE IN MB (adjust this to match the size of your file in MB as above)
$size = 10
$sizes = @(
10,
10
)

# Name of Downloaded file
$localfile = "SpeedTest.bin"

# WEB CLIENT VARIABLES
$webclient = New-Object System.Net.WebClient

#RUN DOWNLOAD & CALCULATE DOWNLOAD SPEED
$downloadstart_time = Get-Date
$webclient.DownloadFile($downloadurl, $localfile)
$downloadtimetaken = $((Get-Date).Subtract($downloadstart_time).Seconds)
$downloadspeed = ($size / $downloadtimetaken) * 8
Write-Output "Time taken: $downloadtimetaken second(s) | Download Speed: $downloadspeed mbps"
# Variable to track if download was successful
$downloadSuccessful = $false

for ($i = 0; $i -lt $downloadurls.Length; $i++) {
$downloadurl = $downloadurls[$i]
$size = $sizes[$i]

# Write-Output "trying $downloadurl"
try {
#RUN DOWNLOAD & CALCULATE DOWNLOAD SPEED
$start_time = Get-Date
# $a = Measure-Command -Expression {
$webclient.DownloadFile($downloadurl, $localfile)
# }
$end_time = Get-Date
$downloadSuccessful = $true
break # exits for loop
} catch {
Write-Output "Failed to download: $downloadurl : Trying the next URL..."
}
}

if (-not $downloadSuccessful) {
Write-Output "All download attempts failed."
exit 1
}


$secs_taken = ($end_time - $start_time).TotalSeconds
$downloadspeed = ($size / $secs_taken) * 8
Write-Output "Time taken: $([Math]::Round($secs_taken, 2)) seconds | Download Speed: $([Math]::Round($downloadspeed, 2)) mbps"


#RUN UPLOAD & CALCULATE UPLOAD SPEED
#$uploadstart_time = Get-Date
Expand All @@ -35,11 +67,9 @@ Remove-Item -path $localfile

#SEND ALERTS IF BELOW MINIMUM THRESHOLD
if ($downloadspeed -ge $mindownloadspeed) {
Write-Output "Speed is acceptable. Current download speed at is $downloadspeed mbps which is above the threshold of $mindownloadspeed mbps"
Write-Output "Speed is acceptable. Current download speed is above the threshold of $mindownloadspeed mbps"
exit 0
}

else {
Write-Output "Current download speed at is $downloadspeed mbps which is below the minimum threshold of $mindownloadspeed mbps"
} else {
Write-Output "Current download speed is below the minimum threshold of $mindownloadspeed mbps"
exit 1
}
Loading