Powershell 是不是;如有的话;在foreach循环中是否存在?

Powershell 是不是;如有的话;在foreach循环中是否存在?,powershell,iis,foreach,http-status-codes,Powershell,Iis,Foreach,Http Status Codes,我试图梳理Web服务器的应用程序池,以检测每个应用程序的HTTP响应代码。我正在使用foreach循环检查响应是否为200,但如果其中一个响应不是200,我需要foreach循环继续并检查所有其他应用程序池 $appPool = Get-WebApplication foreach ($a in $appPool) { $app = $a.Attributes[0].Value; $url = "http://localhost$app/apitest/index" $H

我试图梳理Web服务器的应用程序池,以检测每个应用程序的HTTP响应代码。我正在使用
foreach
循环检查响应是否为200,但如果其中一个响应不是200,我需要
foreach
循环继续并检查所有其他应用程序池

$appPool = Get-WebApplication
foreach ($a in $appPool) {
    $app = $a.Attributes[0].Value;
    $url = "http://localhost$app/apitest/index"
    $HTTP_Request = [System.Net.WebRequest]::Create($url)
    $HTTP_Response = try{
        $HTTP_Request.GetResponse()
    } catch {
        $exceptionMessage = $_.Exception.Message
        $exceptionItem = $app
    }
    $HTTP_Status = [int]$HTTP_Response.StatusCode

    if ($HTTP_Status -eq 200) {
        $errorcode = 0
    } elseif ($HTTP_Status -ne 200) {
        $errorcode = 1
    } else {
        $errorcode = 2
    }
}
我发现任何应用程序池返回什么并不重要,因为循环只会随着最后一个应用程序返回的内容而退出。如果应用程序3返回503,但最后一个应用程序返回200,则
foreach
循环返回200,并以
$errorcode=0
退出


我如何改变这个代码来检查所有的应用程序池,但是如果中间的一个应用程序没有200个状态代码,那么用一个不同的错误代码退出。

一个方法,你可以这样做,就是把返回的布尔值作为一个列表,并检查列表中包含的值。例如:

$results =  foreach($n in 1..10) { 
    $n -eq 5 
}

if ($results -contains $true) {
    Write-Host "There was a 5"
}
以你为例,我想:

$appPool = get-webapplication
$results = foreach($a in $appPool) {

    $app = $a.Attributes[0].Value;
    $url = "http://localhost$app/apitest/index"
    $HTTP_Request = [System.Net.WebRequest]::Create($url)
    $HTTP_Response = try { 
        $HTTP_Request.GetResponse() 
    } catch { 
        $exceptionMessage = $_.Exception.Message
        $exceptionItem = $app
    }
    [int]$HTTP_Response.StatusCode -ne 200
}

if ($results -contains $true) {
    $errorcode = 1
} else {
    $errorcode = 0
}
我不想把这个例子弄得乱七八糟,但实际上我可以这样做:

$errorcode = $results -contains $true -as [int]

一种方法是将返回的布尔值作为一个列表,并检查该列表是否包含该值。例如:

$results =  foreach($n in 1..10) { 
    $n -eq 5 
}

if ($results -contains $true) {
    Write-Host "There was a 5"
}
以你为例,我想:

$appPool = get-webapplication
$results = foreach($a in $appPool) {

    $app = $a.Attributes[0].Value;
    $url = "http://localhost$app/apitest/index"
    $HTTP_Request = [System.Net.WebRequest]::Create($url)
    $HTTP_Response = try { 
        $HTTP_Request.GetResponse() 
    } catch { 
        $exceptionMessage = $_.Exception.Message
        $exceptionItem = $app
    }
    [int]$HTTP_Response.StatusCode -ne 200
}

if ($results -contains $true) {
    $errorcode = 1
} else {
    $errorcode = 0
}
我不想把这个例子弄得乱七八糟,但实际上我可以这样做:

$errorcode = $results -contains $true -as [int]

我会在进入循环之前预先设置
$errorcode
,并且仅当请求的状态代码不是200时才更改其值

$errorcode = 0
foreach ($a in $appPool) {
    ...
    if ($HTTP_Response.StatusCode.value__ -ne 200) {
        $errorcode = 1
    }
}

我会在进入循环之前预先设置
$errorcode
,并且仅当请求的状态代码不是200时才更改其值

$errorcode = 0
foreach ($a in $appPool) {
    ...
    if ($HTTP_Response.StatusCode.value__ -ne 200) {
        $errorcode = 1
    }
}

我喜欢这种方法,因为foreach循环的第一次迭代可能不等于200,所以errorcode将设置为1,所以它不能解决我的问题。但下一次迭代可能等于200,它会将errorcode更改为0。@RaptorPete此方法在循环开始后不会将errorcode重置为0,我认为这是您想要的。@briantist我希望在任何($appPool中的a)返回200以外的值时将$errorcode设置为1。这样,如果此特定服务器上的任何($a in$appPool)出现故障,我将收到警报。例如,如果$appPool中的最后一个$a返回的状态为200,您提供的foreach方法是否会更改回$errorcode=0?@RaptorPete这是我的答案,并且不会在循环中将
$errorcode
重置为0。是什么给你这个主意的?我发布的代码中没有(甚至没有建议)这样做。我喜欢这种方法,因为foreach循环的第一次迭代可能不等于200,所以errorcode将设置为1。但下一次迭代可能等于200,它会将errorcode更改为0。@RaptorPete此方法在循环开始后不会将errorcode重置为0,我认为这是您想要的。@briantist我希望在任何($appPool中的a)返回200以外的值时将$errorcode设置为1。这样,如果此特定服务器上的任何($a in$appPool)出现故障,我将收到警报。例如,如果$appPool中的最后一个$a返回的状态为200,您提供的foreach方法是否会更改回$errorcode=0?@RaptorPete这是我的答案,并且不会在循环中将
$errorcode
重置为0。是什么给你这个主意的?我发布的代码中没有(甚至没有暗示)这样做。