Last active
December 15, 2016 18:30
-
-
Save bogdangrigg/37af848eff49e3cf0b5af918ffaa1d4f to your computer and use it in GitHub Desktop.
http-ping.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################## | |
## Original version @ https://gallery.technet.microsoft.com/scriptcenter/Powershell-Script-for-13a551b3 | |
## Updated: 5/26/2015, Version 2.0, By: Sangamesh (sangameshb@yahoo) on 5/26/2015 | |
############################################################################## | |
param ( | |
[string]$url = 'https://google.com', | |
[switch]$t = $true, | |
[switch]$help | |
) | |
$color = "yellow" | |
if($help -or !$url) { | |
Write-Host " Usage a) (pings 4 times): .\http-ping.ps1 -url https://google.com" -foregroundcolor $color | |
Write-Host " Usage b) (pings unlimted times until stopped): .\http-ping.ps1 -url https://google.com -t" -foregroundcolor $color | |
Write-Host " Usage c) (shows this help): .\http-ping.ps1 -help" -foregroundcolor $color | |
return | |
} | |
$max = 4 | |
if ($t) { | |
$max = -1 | |
} | |
Write-Host "Starting Http-Ping" | |
$index = 0 | |
while($index -lt $max -or $max -eq -1) | |
{ | |
Clear-DnsClientCache | |
Write-Host -NoNewline $url | |
$index = $index + 1 | |
$result = @() | |
$time = try | |
{ | |
$request = $null | |
$result1 = Measure-Command { $request = Invoke-WebRequest -Uri $url -Verbose:$false} | |
$result1.TotalMilliseconds | |
} | |
catch | |
{ | |
<# If the request generated an exception (i.e.: 500 server | |
error or 404 not found), we can pull the status code from the | |
Exception.Response property #> | |
$request = $_.Exception.Response | |
$time = -1 | |
} | |
$result += [PSCustomObject] @{ | |
Time = Get-Date; | |
Uri = $url; | |
StatusCode = [int] $request.StatusCode; | |
StatusDescription = $request.StatusDescription; | |
ResponseLength = $request.RawContentLength; | |
TimeTaken = $time; | |
} | |
$color = "" | |
Foreach($Entry in $Result) { | |
$color = "red" | |
if($Entry.StatusCode -lt 400) | |
{ | |
$color = "green" | |
} | |
Write-Host " HTTP_STATUS=$($Entry.StatusCode) ($($Entry.StatusDescription)) RECEIVED=$($Entry.ResponseLength) Bytes RESPONSE_TIME (Mills)=$($Entry.timetaken)" -foregroundcolor $color | |
} | |
Start-Sleep -s 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment