【发布时间】: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