windows_wiki:powershell_-_speedtest_for_internet_bandwidth

# Powershell - Speedtest For Internet Bandwidth

Used for bandwidth testing, especially useful on server core.

General Information

This script tests download bandwidth via PowerShell, it does so by calling SpeedTest.net.

Checklist

• Check your firewall settings to ensure this is allowed out.
• Internet access. Duh.

Example Code:

speedtest.ps1
```Function downloadSpeed(\$strUploadUrl)
{
\$url = \$topServerUrlSpilt + 'random2000x2000.jpg'
\$col = new-object System.Collections.Specialized.NameValueCollection
\$wc = new-object system.net.WebClient
\$wc.QueryString = \$col
\$string = [System.Text.Encoding]::ASCII.GetString(\$webpage1)
\$downSize = (\$webpage1.length + \$webpage2.length) / 1Mb
}

<#
Using this method to make the submission to speedtest. Its the only way i could figure out how to interact with the page since there is no API.
#>
\$objXmlHttp = New-Object -ComObject MSXML2.ServerXMLHTTP
\$objXmlHttp.Open("GET", "http://www.speedtest.net/speedtest-config.php", \$False)
\$objXmlHttp.Send()

#Retrieving the content of the response.
[xml]\$content = \$objXmlHttp.responseText

<#
Gives me the Latitude and Longitude so i can pick the closer server to me to actually test against. It doesnt seem to automatically do this.
Lat and Longitude for tampa at my house are \$orilat = 27.9238 and \$orilon = -82.3505
This is corroborated against: http://www.travelmath.com/cities/Tampa,+FL - It checks out.
#>
\$oriLat = \$content.settings.client.lat
\$oriLon = \$content.settings.client.lon

#Making another request. This time to get the server list from the site.
\$objXmlHttp1 = New-Object -ComObject MSXML2.ServerXMLHTTP
\$objXmlHttp1.Open("GET", "http://www.speedtest.net/speedtest-servers.php", \$False)
\$objXmlHttp1.Send()

#Retrieving the content of the response.
[xml]\$ServerList = \$objXmlHttp1.responseText

<#
\$Cons contains all of the information about every server in the speedtest.net database.
I was going to filter this to US servers only which would speed this up a lot but i know we have overseas partners we run this against.
Results returned look like this for each individual server:

lat     : 27.9709
lon     : -82.4646
name    : Tampa, FL
country : United States
cc      : US
id      : 1296

#>
\$cons = \$ServerList.settings.servers.server

#Below we calculate servers relative closeness to you by doing some math against latitude and longitude.
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 })

}

\$serverinformation = \$serverinformation | Sort-Object -Property distance

#Runs the functions 4 times and takes the highest result.
\$SpeedResults += @([pscustomobject]@{Speed = \$DLResults1;})

\$SpeedResults += @([pscustomobject]@{Speed = \$DLResults2;})

\$SpeedResults += @([pscustomobject]@{Speed = \$DLResults3;})
{"ft_lang_label":"__label__en","ft_lang_prob":0.57106715,"math_prob":0.7681627,"size":4314,"snap":"2022-27-2022-33","text_gpt3_token_len":1222,"char_repetition_ratio":0.13897912,"word_repetition_ratio":0.015968064,"special_character_ratio":0.28164116,"punctuation_ratio":0.21291208,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95393246,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-16T10:48:55Z\",\"WARC-Record-ID\":\"<urn:uuid:383ce26e-975d-4254-ac95-f0b16209f769>\",\"Content-Length\":\"45630\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9c2dd53f-3bf6-4c1e-9f63-069a9214429e>\",\"WARC-Concurrent-To\":\"<urn:uuid:dc9e51a3-35bc-4e77-b082-b1b853a1ffa6>\",\"WARC-IP-Address\":\"104.251.219.164\",\"WARC-Target-URI\":\"https://owlbearconsulting.com/doku.php?id=windows_wiki:powershell_-_speedtest_for_internet_bandwidth\",\"WARC-Payload-Digest\":\"sha1:UZKVCJMRRGNFHOH2WO24MF7PMXXRQX3V\",\"WARC-Block-Digest\":\"sha1:FWPHNVGPOHK6MMFNLH4LOZCKXLFFJFDG\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882572286.44_warc_CC-MAIN-20220816090541-20220816120541-00295.warc.gz\"}"}