Powershell 基于http响应代码重新启动应用程序池

Powershell 基于http响应代码重新启动应用程序池,powershell,iis,Powershell,Iis,我正在尝试编写一个PowerShell脚本,如果收到503响应代码,该脚本将重新启动IIS中的应用程序池 到目前为止,我已经设法在IIS的默认网站下检索每个crm应用程序的响应代码。但是,我不确定如何查找应用程序池名称。我尝试了下面的方法,但它为每个站点返回相同的应用程序池。有人能帮忙吗 $getSite = (Get-WebApplication -Site 'Default Web Site') $SiteURL = ForEach ($site in $getSite.path) {("h

我正在尝试编写一个PowerShell脚本,如果收到503响应代码,该脚本将重新启动IIS中的应用程序池

到目前为止,我已经设法在IIS的默认网站下检索每个crm应用程序的响应代码。但是,我不确定如何查找应用程序池名称。我尝试了下面的方法,但它为每个站点返回相同的应用程序池。有人能帮忙吗

$getSite = (Get-WebApplication -Site 'Default Web Site')
$SiteURL = ForEach ($site in $getSite.path) {("http://localhost")+$site}
ForEach ($crm in $SiteURL){
$req = [system.Net.WebRequest]::Create($crm)
try {
   $res = $req.GetResponse()
 } catch [System.Net.WebException] {
   $res = $_.Exception.Response
 }
$ApplicationPool = ForEach ($app in $getSite.applicationpool) {$app}  
 if([int]$res.StatusCode -eq 503)  {write-host ($crm + ' ' +  [int]$res.StatusCode) + $app}
 }

我认为您需要访问
响应
属性的
$\ux0.Exception.InnerException

您的
$ApplicationPool
分配没有多大意义,因为您测试的每个
$crm
应用程序只需要一个
ApplicationPool
名称:

foreach($App in @(Get-WebApplication -Site 'Default Web Site')){

    # Uri for the application
    $TestUri = 'http://localhost{0}' -f $App.path

    # Create WebRequest
    $Request = [system.Net.WebRequest]::Create($TestUri)
    try {
        # Get the response
        $Response = $Request.GetResponse()
    } catch [System.Net.WebException] {
        # If it fails, get Response from the Exception
        $Response = $_.Exception.InnerException.Response
    }

    # The numerical value of the StatusCode value is the HTTP status code, ie. 503
    if(503 -eq ($Response.StatusCode -as [int])){
        # Restart the app pool
        Restart-WebAppPool -Name $App.applicationPool
    }
}

嘿,这真的很好用。非常感谢你的帮助。