Skip to content

Instantly share code, notes, and snippets.

@RhysC
Created July 5, 2013 01:43
Show Gist options
  • Save RhysC/5931162 to your computer and use it in GitHub Desktop.
Save RhysC/5931162 to your computer and use it in GitHub Desktop.
Poll URL to see if it is up.
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
}
}
}
@RhysC
Copy link
Author

RhysC commented Jul 5, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment