簡體   English   中英

通過 powershell 或 appcmd 將應用程序池設置為在多個特定時間回收不會在 GUI/IIS 中同時顯示

[英]Setting app pools to recycle at multiple specific times via powershell or appcmd does not display both times in GUI/IIS

我試圖在 iis 8.5 中設置應用程序池以在一天中多次回收,我嘗試使用 powershell 和 app 命令,並且在池中沒有站點/應用程序的服務器上進行測試時,它似乎工作得很好,但是當嘗試在應用程序池中有站點的服務器上使用任一方法進行設置時,我看到奇怪的行為,但是如果我查看應用程序池的回收設置,它似乎在 IIS 的 GUI 中工作,它只顯示其中一個指定的時間。 最初嘗試使用的 Powershell 腳本是:

function Set-ApplicationPoolRecycleTimes {
    param (
        [string]$ApplicationPoolName,
        [string[]]$RestartTimes
    )
    Import-Module WebAdministration
    Write-Output "Updating recycle times for $ApplicationPoolName"
    # Delete all existing recycle times
    Clear-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name Recycling.periodicRestart.schedule    
    Clear-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name Recycling.periodicRestart.time
    foreach ($restartTime in $RestartTimes) {
        Write-Output "Adding recycle at $restartTime"
        # Set the application pool to restart at the time we want
        New-ItemProperty -Path "IIS:\AppPools\$ApplicationPoolName" -Name Recycling.periodicRestart.schedule -Value @{value=$restartTime}
        Set-ItemProperty -Path "IIS:\AppPools\$ApplicationPoolName" -Name Recycling.periodicRestart.time -Value "00:00:00"
    } # End foreach restarttime
} # End function Set-ApplicationPoolRecycleTimes
$apppoolname1 = "app pool's name"
$restartat = @("1:45", "18:45")
Set-ApplicationPoolRecycleTimes -ApplicationPoolName $apppoolname1 -RestartTimes $restartat

同樣,除非應用程序池中有站點,否則這似乎可以完美運行。 當站點存在時,它似乎可以工作,只是 gui 僅顯示設置的時間之一: 在此處輸入圖片說明

但是查詢值顯示兩次:

Import-Module WebAdministration
(Get-ItemProperty ('IIS:\AppPools\app pool name') -Name Recycling.periodicRestart.schedule.collection) | select value

value   
-----   
18:45:00
01:45:00

也嘗試使用 appcmd 但發現相同的結果,在應用程序池中沒有站點的服務器上完美運行,但是當針對具有站點的服務器運行時,在 gui 中丟失了一次,查詢顯示兩次。 我已經打開應用程序池回收的登錄以確認它在兩個時間都發生,但想知道我是否只是忽略了一些明顯的東西。

appcmd 腳本:

CD C:\windows\System32\inetsrv
$V1 = "app pool name"
#clears any existing schedule
cmd.exe /c appcmd.exe set apppool /apppool.name: $V1 /-recycling.periodicRestart.schedule
#setting desired recycles 
cmd.exe /c appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='$v1'].recycling.periodicRestart.schedule.[value='01:45:00']" /commit:apphost
cmd.exe /c appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='$v1'].recycling.periodicRestart.schedule.[value='18:45:00']" /commit:apphost

我在包含站點或不包含站點的應用程序池中嘗試了您的 PowerShell 腳本,這兩種情況您發布的腳本都可以正常工作。

您可以嘗試使用以下腳本:

    function Set-ApplicationPoolRecycleTimes {

    param (
        [string]$ApplicationPoolName,
        [string[]]$RestartTimes
    )

    Import-Module WebAdministration

    Write-Output "Updating recycle times for $ApplicationPoolName"

    # Delete all existing recycle times
    Clear-ItemProperty IIS:\AppPools\$ApplicationPoolName -Name Recycling.periodicRestart.schedule

    foreach ($restartTime in $RestartTimes) {

        Write-Output "Adding recycle at $restartTime"
        # Set the application pool to restart at the time we want
        New-ItemProperty -Path "IIS:\AppPools\$ApplicationPoolName" -Name Recycling.periodicRestart.schedule -Value @{value=$restartTime}

    } # End foreach restarttime

} # End function Set-ApplicationPoolRecycleTimes
$apppoolname = "abcsite"
$restartat = @("05:55", "12:55", "17:00")

Set-ApplicationPoolRecycleTimes -ApplicationPoolName $apppoolname -RestartTimes $restartat

在此處輸入圖片說明

或者

appcmd.exe set config  -section:system.applicationHost/applicationPools /+"[name='test1'].recycling.periodicRestart.schedule.[value='07:00:00']" /commit:apphost

    appcmd.exe set config  -section:system.applicationHost/applicationPools /+"[name='test1'].recycling.periodicRestart.schedule.[value='18:25:00']" /commit:apphost

或者

 Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='test1']/recycling/periodicRestart/schedule" -name "." -value @{value='07:00:00'}

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/applicationPools/add[@name='test1']/recycling/periodicRestart/schedule" -name "." -value @{value='18:25:00'}

以上腳本在 IIS 10 (Windows 10) 和 IIS 8.5(windows server 2012r2) 上進行了測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM