PowerShell: Test URL for Reachability

function testUrl($url){
    try{
        $HTTP_Request = [System.Net.WebRequest]::Create($url)
        $HTTP_Response = $HTTP_Request.GetResponse()
        [int]$HTTP_Status = [int]$HTTP_Response.StatusCode
        If ($HTTP_Status -eq 200) {
            Write-Host "Url is reachable!" -ForegroundColor Green
            $reachable=$true
        }Else {
            Write-warning "$url is NOT unreachable!"
            $reachable=$false
        }
        If ($null -ne $HTTP_Response) {
            $HTTP_Response.Close();
        }
        return $reachable
    }catch{
        write-warning $_
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *