Skip to content

Instantly share code, notes, and snippets.

@d4rkeagle65
Last active June 23, 2020 15:06
Show Gist options
  • Save d4rkeagle65/f3cf489f8a79b18598e2a4036b8ba6a8 to your computer and use it in GitHub Desktop.
Save d4rkeagle65/f3cf489f8a79b18598e2a4036b8ba6a8 to your computer and use it in GitHub Desktop.
Runs a speed test to the closest few speedtest.net servers and returns the highest result.
Function Invoke-SpeedTest {
param (
$Server
)
$topServerUrlSpilt = $Server -split 'upload'
$url = $topServerUrlSpilt[0] + 'random2000x2000.jpg'
$wc = New-Object system.net.WebClient
$wc.QueryString = New-Object System.Collections.Specialized.NameValueCollection
$downloadElaspedTime = (Measure-Command {$webpage1 = $wc.DownloadData($url)}).totalseconds
$downloadSize = [Math]::Round(($webpage1.length / 1Mb), 2)
$downSpeed = ($downloadSize / $downloadElaspedTime) * 8
$downloadSpeed = [Math]::Round($downSpeed, 2)
return $downloadSpeed
}
Function Get-GeoLocationLatLong {
$objXmlHttp = New-Object -ComObject 'MSXML2.ServerXMLHTTP'
$objXmlHttp.Open("GET", "http://www.speedtest.net/speedtest-config.php", $False)
$objXmlHttp.Send()
[xml]$content = $objXmlHttp.responseText
return $content.settings.client
}
Function Get-ClosestSpeedTestServer {
$client_geoLoc = Get-GeoLocationLatLong
$oriLat = $client_geoLoc.lat
$oriLon = $client_geoLoc.lon
$objXmlHttp1 = New-Object -ComObject MSXML2.ServerXMLHTTP
$objXmlHttp1.Open("GET", "http://www.speedtest.net/speedtest-servers.php", $False)
$objXmlHttp1.Send()
[xml]$ServerList = $objXmlHttp1.responseText
$cons = $ServerList.settings.servers.server
foreach($val in $cons)
{
$R = 6371;
[float]$dlat = ([float]$oriLat - [float]$val.lat) * 3.14 / 180;
[float]$dlon = ([float]$oriLon - [float]$val.lon) * 3.14 / 180;
[float]$a = [math]::Sin([float]$dLat/2) * [math]::Sin([float]$dLat/2) + [math]::Cos([float]$oriLat * 3.14 / 180 ) * [math]::Cos([float]$val.lat * 3.14 / 180 ) * [math]::Sin([float]$dLon/2) * [math]::Sin([float]$dLon/2);
[float]$c = 2 * [math]::Atan2([math]::Sqrt([float]$a ), [math]::Sqrt(1 - [float]$a));
[float]$d = [float]$R * [float]$c;
$ServerInformation +=
@([pscustomobject]@{Distance = $d; Country = $val.country; Sponsor = $val.sponsor; Url = $val.url })
}
return $ServerInformation
}
Function Invoke-WANDownloadSpeedTest {
param(
$Attempts = 4
)
$serverInfo = Get-ClosestSpeedTestServer
$aCounter = $Attempts
$results = @()
Do {
$dlResult = Invoke-SpeedTest -Server $serverInfo[$aCounter].url
$results += @([pscustomobject]@{Speed = $dlResult;})
$aCounter -= 1
} While ($aCounter -ge 0)
$WANSpeed = $results | Sort-Object -Property Speed -Descending | Select-Object -First 1
return $WANSpeed | Select -ExpandProperty Speed
}
Invoke-WANDownloadSpeedTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment