Link to home
Start Free TrialLog in
Avatar of SnAkEhIpS
SnAkEhIpSFlag for United States of America

asked on

PowerShell to determine mirror site with least latency.

I've got an interesting one for all the PowerShell crew. I often go to websites for a download of an ISO. The site offers many mirror URLs. What would be cool is to copy those URLs to the clipboard and pipe them to a PS script that will attempt to determine which mirror has the least latency. Any gurus out there willing to bite? I think many techs would appreciate such a script/tool in their arsenal.
Avatar of Michael B. Smith
Michael B. Smith
Flag of United States of America image

Here ya go:

## .\Get-WebSiteSpeed.ps1 -URLs http://www.microsoft.com, http://www.yahoo.com, http://www.google.com

Param(
    [Parameter( Mandatory = $false )]
    [ValidateNotNullOrEmpty()]
    [Alias( 'URL', 'URI', 'URIs' )]
    [String[]] $URLs,
    
    [Parameter( Mandatory = $false )]
    [Switch] $Tls12 = $false
)

function wh
{
    Param
    (
        $object,
        $ForeGroundColor = 'White'
    )

    Write-Host -Object $object -ForegroundColor $ForeGroundColor
}

##
## Main
##

if( 0 )
{
    ## i have traditionally needed the below workarounds, but they just get in the way in this case

    ## accept any SSL certificate, even expired or otherwise invalid certs
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

    ## force TLS 1.2
    if( $Tls12 )
    {
        [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
    }
}

foreach( $url in $URLs )
{
    wh "Checking $url"

    $time = Measure-Command -Expression { $null = Invoke-WebRequest -Uri $url }

    $ms = $time.TotalMilliseconds

    "Checking $url took $( $ms.ToString( 'n0' ) ) ms"
}

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.