重新检查URL三次以了解web异常情况powershell

重新检查URL三次以了解web异常情况powershell,powershell,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_wai

你在找这样的东西

$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)

附:我刚刚开始回答stackoverflow的问题。我希望听到您的反馈。

我正在寻找,在重新启动服务器中的服务之前,请再次检查URL三次。当URL显示web异常时,它没有检查三次,我想在URL关闭时检查三次。请帮助我编辑问题并在那里添加问题描述。到目前为止,理解这个问题还需要阅读评论部分。评论是,但有点像是小观察的便笺。请看:这是一个很好的经验法则,如果你看到一个你觉得不符合SO准则的离题问题,即使你知道如何回答它,也要克制自己并标记它们。如果我们回答离题的问题,它会鼓励SO中的不良行为,而这不是SO创建的初衷。有关这些guidline的一些好文章,请参阅和
#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"
}