首页 > 解决方案 > 重新检查 URL 三次以查找 Web 异常案例 powershell

问题描述

$status_wait =1    
Do{
  $https_request= invoke_webrequest -uri "https:\\"
  $https_status = [int]http_request.statuscode

  If($($_.categoryinfo.reason) -eq "webexception") {
    Start-sleep -secounds 30
    $status_wait+=1
  }
  Elseif($http_status -eq 200) {
    Write-host "URL is up"
  }
}Untill (status_wait -eq 3)

标签: powershell

解决方案


您正在寻找这样的东西吗?

#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:我刚刚开始在stackoverflow上回答。我很想听听您的反馈。


推荐阅读