Powershell 循环URL检查加载时间和状态代码的脚本

Powershell 循环URL检查加载时间和状态代码的脚本,powershell,Powershell,我有点被这个powershell脚本卡住了 我有一个IIS 10 Web服务器,在默认网站下有多个应用程序 我需要遍历一些应用程序,检查它是否在3秒内响应,并检查它是否返回StatusCode=200。 如果不是,则回收“有问题”应用程序的应用程序池,并返回“状态关键”状态 很明显,我做错了什么,因为这不正常。有什么建议吗 $StateOK = 0 $StateCritical = 2 $infTable = @{ URL1 = 'http://myServer/sub/appname1.s

我有点被这个powershell脚本卡住了

我有一个IIS 10 Web服务器,在默认网站下有多个应用程序

我需要遍历一些应用程序,检查它是否在3秒内响应,并检查它是否返回StatusCode=200。 如果不是,则回收“有问题”应用程序的应用程序池,并返回“状态关键”状态

很明显,我做错了什么,因为这不正常。有什么建议吗

$StateOK = 0
$StateCritical = 2

$infTable = @{
 URL1 = 'http://myServer/sub/appname1.svc'
 URL2 = 'http://myServer/sub/appname2.svc'
 URL3 = 'http://myServer/sub/appname3.svc'
 URL4 = 'http://myServer/sub/appname4.svc'
 URL5 = 'http://myServer/sub/appname5.svc'   
}
foreach ($key in $infTable.GetEnumerator()) {
           $M = Measure-Command -Expression {(Invoke-WebRequest -Uri $($key.Value))
           if ($M.TotalMilliseconds -gt 3000 ){
                    write-host "$($key.Name) is not available"
                    Restart-WebAppPool -Name $($key.Name)
                    return $StateCritical  
            }

            elseif ($M.TotalMilliseconds -lt 3000 ){
                    write-host "$($key.Name) is OK"
                    return $StateOK  
            }
}}

如果这仍然引起某人的兴趣,我设法在他的帮助下解决了这个问题

$infTable=@{
URL1=http://myServer/sub/appname1.svc'
URL2=http://myServer/sub/appname2.svc'
URL3=http://myServer/sub/appname3.svc'
URL4=http://myServer/sub/appname4.svc'
URL5=http://myServer/sub/appname5.svc'
}
$Result=@()
foreach($infTable.GetEnumerator()中的键){
$time=try{
$request=$null
##请求URI,并测量响应所用的时间。
$result1=Measure命令{$request=Invoke WebRequest-Uri$($key.Value)-TimeoutSec 4}
$result1.0毫秒
} 
抓住
{
$request=$\异常.Response
$request
$time=-1
}  
$result+=[PSCustomObject]@{
时间=获取日期;
#Uri=$Uri;
Uri=$($key.Value);
StatusCode=[int]$request.StatusCode;
StatusDescription=$request.StatusDescription;
ResponseLength=$request.RawContentLength;
耗时=$time;
}
}
#准备HTML格式的电子邮件正文
如果($result-ne$null)
{

$Outputreport="网站可用性报告网站可用性报告UrlStatusCodeStatusDescription ResponseLengthTimetaken在表达式之外,只需分配
$response=Invoke WebRequest-Uri$key.Value
。然后在
if else
块中选中
$response.StatusCode
。问题是当其中一个URL没有响应时脚本被卡在该URL上。我需要一种方法来告诉脚本尝试3秒钟,如果没有响应,则返回'StateCritical'try with
Invoke Webrequest-Timeoutsec 3
。我刚刚注意到,我得到了所有URL的状态码200。即使它没有应答或给出错误404
$infTable = @{
    URL1 = 'http://myServer/sub/appname1.svc'
    URL2 = 'http://myServer/sub/appname2.svc'
    URL3 = 'http://myServer/sub/appname3.svc'
    URL4 = 'http://myServer/sub/appname4.svc'
    URL5 = 'http://myServer/sub/appname5.svc'
}

 $Result = @()

  foreach ($key in $infTable.GetEnumerator()) {
      $time = try{
      $request = $null
       ## Request the URI, and measure how long the response took.
      $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $($key.Value) -TimeoutSec 4 }
      $result1.TotalMilliseconds
      } 
      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 #>
       $request = $_.Exception.Response
       $request
       $time = -1
      }  
  $result += [PSCustomObject] @{
  Time = Get-Date;
  #Uri = $uri;
  Uri = $($key.Value);
  StatusCode = [int] $request.StatusCode;
  StatusDescription = $request.StatusDescription;
  ResponseLength = $request.RawContentLength;
  TimeTaken =  $time; 
  }

}
    #Prepare email body in HTML format
if($result -ne $null)
{
    $Outputreport = "<HTML><TITLE>Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD</TR>"
    Foreach($Entry in $Result)
    { 
        if($Entry.StatusCode -ne "200")
        {
            $Outputreport += "<TR bgcolor=red>"
        }

        elseif($Entry.StatusCode -eq "500")
        { 
            $Outputreport += "<TR bgcolor=purple>"
        }
        else
        {
            $Outputreport += "<TR>"
        }
        $Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
    }
    $Outputreport += "</Table></BODY></HTML>"
}

$Outputreport | out-file C:\Test.htm
Invoke-Expression C:\Test.htm