Quantcast
Channel: PowerShell General
Viewing all articles
Browse latest Browse all 10624

I write powershell script for internet test speed but it is working properly in india but not working in us,uk etc.

$
0
0

I am given my script below-

kindly change and suggest me 

=====================

param(

[string] $paramSponsor,

[int] $paramRepeat,

[int] $paramDownload,

[int] $paramUpload

)

 

$StopWatch = New-Object system.Diagnostics.Stopwatch  

write-host "Retrieving Speedtest.net configuration..."

 

$objXmlHttp = New-Object -ComObject MSXML2.ServerXMLHTTP

$objXmlHttp.Open("GET", "http://www.speedtest.net/speedtest-config.php", $False)

$objXmlHttp.Send()

[xml]$content = $objXmlHttp.responseText

 

$oriLat = $content.settings.client.lat

$oriLon = $content.settings.client.lon

 

write-host " "

write-host "Retrieving speedtest.net server list..."

write-host " "

write-host "------------------------------------------"

write-host " "

 

$objXmlHttp1 = New-Object -ComObject MSXML2.ServerXMLHTTP

$objXmlHttp1.Open("GET", "http://www.speedtest.net/speedtest-servers.php", $False)

$objXmlHttp1.Send()

[xml]$content1 = $objXmlHttp1.responseText

$cons = $content1.settings.servers.server 

 

$sortKm = @()

$sortCountry = @()

$sortSponsor = @()

$sortUrl = @()

$arrayTopKms = @()

$arrayTopSponsor = @()

$arrayTopUrl = @()

$bestServer = @{}

$downloadOutputArray = @{}

$uploadOutputArray = @{}

 

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;

 

$sortKm += $d

$sortCountry += $val.country

$sortSponsor += $val.sponsor

$sortUrl += $val.url

}

 

$strListVal = 5

$n = $sortKm.length

Do {

$boolChanged = $false

for ($i = 0 ; $i -lt ($n - 1); $i++) {

if ($sortKm[$i] -gt $sortKm[$i+1]) {

$sortKm[$i], $sortKm[$i+1] = $sortKm[$i+1], $sortKm[$i]

$sortCountry[$i], $sortCountry[$i+1] = $sortCountry[$i+1], $sortCountry[$i]

$sortSponsor[$i], $sortSponsor[$i+1] = $sortSponsor[$i+1], $sortSponsor[$i]

$sortUrl[$i], $sortUrl[$i+1] = $sortUrl[$i+1], $sortUrl[$i]

$boolChanged = $true

}

}

$n -= 1

}

Until (-not $boolChanged)

 

$counter = 0

foreach($Km in $sortKm){

if($counter -lt $strListVal){

write-host "Distance (Km) : "  $Km  " | Country :" $sortCountry[$counter] " | Sponsors :" $sortSponsor[$counter]

$arrayTopSponsor += $sortSponsor[$counter]

$arrayTopUrl += $sortUrl[$counter]

}

$counter = $counter + 1

 

$j = 0

$sponsorExists = 0

if($paramSponsor.length -ne 0){

$paramSponsor = $paramSponsor -replace "\s", ""

foreach($findSponsor in $sortSponsor){

$findSponsor = $findSponsor -replace "\s", ""

if($findSponsor.toLower() -eq $paramSponsor.toLower()){

$strUploadUrl = $sortUrl[$j]

$strUploadSponsor = $sortSponsor[$j]

$sponsorExists = 1

}

$j = $j + 1

}

Write-Host ""

Write-Host "Performing speedtest using" $strUploadSponsor

if($sponsorExists -eq 0){

$host.ui.WriteErrorLine('Invalid Sponsor name.')

[Environment]::Exit("10010")

}

}

else{

$strUploadUrl = $arrayTopUrl[0]

$strUploadSponsor = $arrayTopSponsor[0]

Write-Host ""

Write-Host "Performing speedtest using " $strUploadSponsor

}

 

Function downloadSpeed($strUploadUrl){

$topServerUrlSpilt = $strUploadUrl -split 'upload'

$url4000 = $topServerUrlSpilt[0] + 'random350x350.jpg'

$url4001 = $topServerUrlSpilt[0] + 'random350x350.jpg'

$col = new-object System.Collections.Specialized.NameValueCollection 

$wc = new-object system.net.WebClient 

$wc.QueryString = $col 

$StopWatch.Start()

$webpage1 = $wc.DownloadData($url4000)

$StopWatch.stop()

$downloadElaspedTime1 = $StopWatch.ElapsedMilliseconds

$StopWatch.reset()

$StopWatch.Start()

$webpage2 = $wc.DownloadData($url4001)

$StopWatch.stop()

$downloadElaspedTime2 = $StopWatch.ElapsedMilliseconds

$StopWatch.reset()

$downloadElaspedTime = $downloadElaspedTime1 + $downloadElaspedTime2

$string = [System.Text.Encoding]::ASCII.GetString($webpage1)

$downSize = ($webpage1.length + $webpage2.length) / 1Mb

$downloadSize = [Math]::Round($downSize, 2)

$downloadTimeSec = $downloadElaspedTime * 0.001

$downSpeed = ($downloadSize / $downloadTimeSec) * 8

$downloadSpeed = [Math]::Round($downSpeed, 2)

write-host " "

write-host "------------------------------------------"

write-host " "

write-host "Download Size in Mb : " $downloadSize

write-host "Download Time in seconds : " $downloadTimeSec

write-host "Download Speed Mbit/sec : " $downloadSpeed

write-host " "

write-host "------------------------------------------"

write-host " "

$downloadOutputArray[0] = $downloadSpeed

$downloadOutputArray[1] = $string

return $downloadOutputArray

}

 

$stringData = downloadSpeed($strUploadUrl)

 

Function uploadSpeed($stringData){

$url = $strUploadUrl

$postData = "content1=" + $stringData[1]

$wc = new-object system.net.WebClient 

$wc.Headers.Add("Content-Type","application/x-www-form-urlencoded") 

[byte[]]$byteArray = [System.Text.Encoding]::ASCII.GetBytes($postData) 

$StopWatch.Start()

$webpage1 = $wc.UploadData($url,"POST",$byteArray); 

$response = [System.Text.Encoding]::ASCII.GetString($webpage1) 

$uploadElaspedTime = $StopWatch.ElapsedMilliseconds

$StopWatch.stop()

$StopWatch.reset()

$uploadSplit = $response -split '='

$upSize = $stringData[1].length / 1Mb

$uploadSize = [Math]::Round($upSize, 2)

$uploadTimeSec = $uploadElaspedTime * 0.001

$upSpeed = ($uploadSize / $uploadTimeSec) * 8

$uploadSpeed = [Math]::Round($upSpeed, 2) 

write-host "Upload Size in Mb : " $uploadSize

write-host "Upload Time in Seconds : " $uploadTimeSec

write-host "Upload Speed Mbit/sec : " $uploadSpeed

write-host " "

$uploadOutputArray[0] = $uploadSpeed

return $uploadOutputArray

}

 

$strUploadResult = uploadSpeed($stringData)

 

if($paramRepeat.length -ne 0){

if($paramRepeat -eq 1){

if(($stringData[0] -lt $paramDownload) -or ($strUploadResult[0] -lt $paramUpload)){

write-host "Download speed is below a tolerable threshold."

write-host "Re-Testing the download and upload speed."

$stringData = downloadSpeed($strUploadUrl)

$strUploadResult = uploadSpeed($stringData)

if(($stringData[0] -lt $paramDownload) -or ($strUploadResult[0] -lt $paramUpload)){

if($stringData[0] -lt $paramDownload){

$host.ui.WriteErrorLine('Download speed is below a tolerable threshold.')

[Environment]::Exit("10010")

}

if($strUploadResult[0] -lt $paramUpload){

$host.ui.WriteErrorLine('Upload speed is below a tolerable threshold.')

[Environment]::Exit("10010")

}

}

$paramRepeat = 0

}

}

}


Viewing all articles
Browse latest Browse all 10624

Latest Images

Trending Articles



Latest Images