[英]How to recycle appool when IIS getting slow response by using Powershell?
我正在嘗試編寫腳本來檢查Web應用程序的運行狀況。 例如,如果我在10秒鍾內沒有收到消息,則必須使用Powershell來回收我的apppool。或者除了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 + Restart-WebAppPool-名稱“ app.xxx.com” + ~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~ + + CategoryInfo:InvalidOperation:(:) [Restart-WebAppPool],InvalidOperationException + FullyQualifiedErrorId:InvalidOperation,Microsoft.IIs.PowerShell.Provider.RestartAppPoolCommand
看來您應該在嘗試重新啟動應用程序池之前檢查其狀態。
if ((Get-WebAppPoolState -Name "app.xxx.com").Value -eq "Stopped") {
Start-WebAppPool -Name "app.xxx.com"
}
else {
Restart-WebAppPool -Name "app.xxx.com"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.