[Solved] Recheck the URL thrice for web exception cases powershell


You are looking for something like this ?

#Remove-Variable * -ErrorAction SilentlyContinue

$url = "www.yourtargetsite.com"
    
function try-webrequest($url){
    try{
        return (Invoke-WebRequest -uri $url -ErrorAction Stop)
    }catch{
        return $_
    }
}
    
$wr = try-webrequest $url
    
if ($wr.statuscode -ne 200){        
    If($wr.categoryinfo.reason -eq "webexception"){
        $count = 1
        while($count -lt 4){
            "Retrying...$count"
            $count++
            if ((try-webrequest $url).statuscode -eq 200){break}
            sleep -seconds 30 
        }
    }else{
        #In case you want to handle any other exception
        "The link is down NOT due to webexception"
    }
}else{
    "The link's up"
}

PS: I have just started answering on stackoverflow. I’d love to hear your feedback.

2

solved Recheck the URL thrice for web exception cases powershell