尝试通过 Powershell 脚本启动应用程序池 - 间歇性出错

Bas*_*e33 20 windows iis windows-server-2012-r2

我有一个批处理脚本,它允许我关闭站点、部署文件并重新打开站点。

  1. 停止应用程序池 - 有效
  2. 停止网站 - 有效
  3. 部署文件 - 有效
  4. 启动应用程序池 - 仅有时有效!
  5. 启动网站 - 如果以前的作品有效

我正在运行 Windows Server 2012 R2,批处理脚本由 Octopus Deploy 触手执行。

它失败的线路是:

 Start-WebAppPool -Name $appPoolName
Run Code Online (Sandbox Code Playgroud)

$appPoolName 在哪里 live.website.com

这条线有时有效,但在其他情况下无效,并且在任何模式中都不一致。

我在其他服务器上有相同的脚本。我检查了应用程序信息服务是否正在运行并且运行良好。事件查看器中没有系统日志。

虽然,我有一个应用程序错误,它在调用 Start-WebAppPool 时引发:

ERROR  + Start-WebAppPool -Name $appPoolName
ERROR  start-webitem : The service cannot accept control messages at this time. 
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会发生这种情况?我试图编写一个 do-while 循环,直到它处于“已启动”状态,但它永远循环失败。

更新

原来当我关闭应用程序池时,进程并没有停止。

为什么在停止应用程序池后进程会继续运行?它实际上继续运行,没有停止。

固定的!

所以 - 按照下面的评论,当我停止应用程序池时,我现在确保它在继续脚本之前完全处于停止状态。

这是我现在拥有的脚本并且完全正常工作:

# Load IIS module:
Import-Module WebAdministration

# Get AppPool Name
$appPoolName = $OctopusParameters['appPoolName']

if ( (Get-WebAppPoolState -Name $appPoolName).Value -eq "Stopped" )
{
    Write-Host "AppPool already stopped: " + $appPoolName
}
else
{
    Write-Host "Shutting down the AppPool: " + $appPoolName
    Write-Host (Get-WebAppPoolState $appPoolName).Value

# Signal to stop.
Stop-WebAppPool -Name $appPoolName
}

do
{
    Write-Host (Get-WebAppPoolState $appPoolName).Value
    Start-Sleep -Seconds 1
}
until ( (Get-WebAppPoolState -Name $appPoolName).Value -eq "Stopped" )
Run Code Online (Sandbox Code Playgroud)

spi*_*hie 3

Octopus Deploy 有一些社区 PowerShell 脚本,您可以在此处找到:https://library.octopus.com/listing

这是其中之一的内容,其中有重试:

# Load IIS module:
Import-Module WebAdministration

# Get AppPool Name
$appPoolName = $OctopusParameters['appPoolName']
# Get the number of retries
$retries = $OctopusParameters['appPoolCheckRetries']
# Get the number of attempts
$delay = $OctopusParameters['appPoolCheckDelay']

# Check if exists
if(Test-Path IIS:\AppPools\$appPoolName) {

    # Stop App Pool if not already stopped
    if ((Get-WebAppPoolState $appPoolName).Value -ne "Stopped") {
        Write-Output "Stopping IIS app pool $appPoolName"
        Stop-WebAppPool $appPoolName

        $state = (Get-WebAppPoolState $appPoolName).Value
        $counter = 1

        # Wait for the app pool to the "Stopped" before proceeding
        do{
            $state = (Get-WebAppPoolState $appPoolName).Value
            Write-Output "$counter/$retries Waiting for IIS app pool $appPoolName to shut down completely. Current status: $state"
            $counter++
            Start-Sleep -Milliseconds $delay
        }
        while($state -ne "Stopped" -and $counter -le $retries)

        # Throw an error if the app pool is not stopped
        if($counter -gt $retries) {
            throw "Could not shut down IIS app pool $appPoolName. `nTry to increase the number of retries ($retries) or delay between attempts ($delay milliseconds)." }
    }
    else {
        Write-Output "$appPoolName already Stopped"
    }
}
else {
    Write-Output "IIS app pool $appPoolName doesn't exist"
}
Run Code Online (Sandbox Code Playgroud)

它来自这个库模板https://library.octopus.com/step-templates/3aaf34a5-90eb-4ea1-95db-15ec93c1e54d/actiontemplate-iis-apppool-stop