【问题标题】:How to recycle appool when IIS getting slow response by using Powershell?当使用 Powershell 响应缓慢时如何回收应用程序池?
【发布时间】:2019-10-24 01:13:52
【问题描述】:

我正在尝试编写一个脚本来检查我的 Web 应用程序的运行状况。例如,如果我无法在 10 秒内收到消息,我必须使用 Powershell 回收我的应用程序池。或者除了 200-ok 代码之外,我的应用程序池应该回收。

请看下面的代码和错误:

# Load IIS module:
Import-Module WebAdministration
while ($true) {
    write-host 'Runnig For Check app.xxx.com ...'

# First we create the request.
$HTTP_Request = [System.Net.WebRequest]::Create('https://app.xxx.com/')
Try
{
# We then get a response from the site.
$HTTP_Response = $HTTP_Request.GetResponse()

# We then get the HTTP code as an integer.
$HTTP_Status = [int]$HTTP_Response.StatusCode

If ($HTTP_Status -eq 200) {
    Write-Host "Site is OK!"
}
Else {
    Write-Host "The Site may be down, please check!"
    Restart-WebAppPool -Name "app.xxx.com"
}
}
Catch
{
     Stop-WebAppPool -Name "app.xxx.com"
     Restart-WebAppPool -Name "app.xxx.com"
}

# Finally, we clean up the http request by closing it.
$HTTP_Response.Close()

 Start-Sleep -Seconds 120
}

错误:

Restart-WebAppPool : 你必须先启动停止的对象 重新启动它。在 C:\Scripts\CheckHealthHaydigo.ps1:25 char:6 +重启-WebAppPool-名称“app.xxx.com” + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Restart-WebAppPool], InvalidOperationException + FullyQualifiedErrorId : InvalidOperation,Microsoft.IIs.PowerShell.Provider.RestartAppPoolCommand

【问题讨论】:

  • 为什么不在 Start-WebAppPool 停止时运行它而不是尝试重新启动它?

标签: powershell devops


【解决方案1】:

您似乎应该在尝试重新启动应用程序池之前检查它的状态。

if ((Get-WebAppPoolState -Name "app.xxx.com").Value -eq "Stopped") {
    Start-WebAppPool -Name "app.xxx.com"
}
else {
    Restart-WebAppPool -Name "app.xxx.com"
}

【讨论】:

    猜你喜欢
    • 2010-10-10
    • 1970-01-01
    • 2011-08-31
    • 2015-03-30
    • 2016-09-20
    • 2015-01-07
    • 1970-01-01
    • 2011-09-20
    • 2016-06-13
    相关资源
    最近更新 更多