通过命令行回收 IIS 中的所有应用程序池

Recycle all apps pools in IIS by command line

我有一个 PowerShell 脚本:

& $psexec $serveraddr -u $remoteuser -p $remotepass -accepteula C:\Windows\System32\inetsrv\appcmd.exe list apppool /xml | C:\Windows\System32\inetsrv\appcmd.exe recycle apppool /in

我正在使用它来回收所有 IIS 池。问题是只有从 IIS 池中给出的默认值才会被回收。没有私人游泳池被回收。第二个 appcmd 找不到它们。首先 appcmd 查找 IIS 和私有给定的所有池。

错误是:

ERROR ( message:Nie można odnaleźć obiektu APPPOOL o identyfikatorze "Core1". )

波兰语是:

ERROR ( message: Can't find object APPPOOL with id "Core1". )

我无法回收专用池。有没有办法绕过这个?

所以命令的第二部分是在本地执行的。我已将脚本更改为通过单个命令回收每个池:

& $psexec $server -u $remoteuser -p $remotepass -accepteula C:\Windows\System32\inetsrv\appcmd.exe recycle apppool /apppool.name:Core1

这是一个回收站 所有 应用程序池:

& $env:windir\system32\inetsrv\appcmd list apppools /state:Started /xml | & $env:windir\system32\inetsrv\appcmd recycle apppools /in

这对您的问题来说有点矫枉过正,但您可能对在多台服务器上并行执行某些操作的一般替代方法感兴趣:

$servers=@('server1', 'server2', 'server3')

$recycleAppPools = {
    echo $(Get-Wmiobject -Class Win32_ComputerSystem).Name
    appcmd list apppools /state:Started /xml | appcmd recycle apppools /in
    echo "`n"
}

workflow Perform-Deployment {
    Param ($servers, $actionBlock)

    # Run on all servers in parallel
    foreach -parallel ($server in $servers) {
        "Doing on $server..."
        # Execute script on the server
        InlineScript {
            $scriptBlock = [scriptblock]::Create($Using:actionBlock)
            Invoke-Command -computername $Using:server -ScriptBlock $scriptBlock
        }
    }
}

cls
# Execute workflow
Perform-Deployment $servers $recycleAppPools

此外,您可以将参数传递给您的脚本块,例如:

$DeployPythonPackage = {
    param($venv, $pythonPackagePath)
    & "$venv\scripts\pip" install --upgrade $pythonPackagePath
}

workflow Perform-Deployment {
    Param ($servers, $actionBlock, $venv, $pythonPackagePath)

    # Run on all servers in parallel
    foreach -parallel ($server in $servers) {
        "Deploying Python package '$pythonPackagePath' on $server..."
        # Execute script on the server
        InlineScript {
            $scriptBlock = [scriptblock]::Create($Using:actionBlock)
            Invoke-Command -computername $Using:server -ScriptBlock $scriptBlock `
                -ArgumentList $Using:venv, $Using:pythonPackagePath
        }
    }
}

cls
# Execute workflow
Perform-Deployment $servers $DeployPythonPackage $venv $pythonPackagePath