【问题标题】:Restart application pool based on http response code根据http响应码重启应用程序池
【发布时间】:2015-10-08 03:08:15
【问题描述】:

我正在尝试编写一个 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}
 }

【问题讨论】:

    标签: powershell iis


    【解决方案1】:

    我认为您需要访问 $_.Exception.InnerException 以获取 Response 属性。

    您的$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
        }
    }
    

    【讨论】:

    • 嘿,这很好用。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 2012-05-24
    • 2010-09-19
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2016-02-02
    • 2012-03-28
    相关资源
    最近更新 更多