Created
July 5, 2013 01:43
-
-
Save RhysC/5931162 to your computer and use it in GitHub Desktop.
Poll URL to see if it is up.
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
function Poll-Website([string]$uri){ | |
$status = "" | |
$retryCount = 0; | |
while($status -ne "OK" -and $retryCount -le 5){ | |
try{ | |
$status = (Invoke-WebRequest -Uri $uri).StatusDescription | |
Write-Host $status | |
} | |
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 #> | |
$status = $_.Exception.Response.StatusDescription | |
Write-Host $status | |
$retryCount++ | |
sleep -Seconds 5 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Invoke-webrequest is ps 3.0, use older techniques for pre PS 3:
$web = New-Object System.Net.WebClient
$response = $web.DownloadString($uri) #throws on error code