Technorati Tags: ,

One of the ways the Microsoft IT team monitors performance of our internal SharePoint servers is using a tool which calculates time-to-first-byte for a client-generated HTTP request (code at the end of this whitepaper). This gives us a client-based perspective of how long pages are taking to load.

It doesn’t take much to create a PowerShell function that does the same thing.

Pass an array of strings representing URLs and you’ll get a table with duration, start, and end times:

$array = @( "http://www.bing.com", "http://www.google.com", "http://www.facebook.com")
$array | Ping-Url

Or just call Ping-Url with one string parameter representing the URL

Ping-Url "http://www.bing.com"

Here's the actual script with the function:

function Ping-Url {
    param(
        [Parameter(ValueFromPipeline=$true)][string] $url
    )
    process {
        $request = [System.Net.WebRequest]::Create( $url )
        $request.UseDefaultCredentials = $true
        $request.Timeout = 90000;

        $startTime = Get-Date
        $request.GetResponse() > $null
        $stopTime = Get-Date

        $object = New-Object PSObject -Property @{
            Url           = $request.RequestUri
            Duration      = $stopTime-$startTime
            StartTime     = $startTime
            EndTime       = $stopTime
        } | Select-Object Url, Duration, StartTime, EndTime  # to ensure order
       
        $object
    }
}