Recycling IIS application pool using PowerShell: “

2019-05-13 22:00发布

问题:

It looks like a recent windows update has broken some functionality I was using to recycle IIS6 application pools, as this has been working for months up to today.

Exception calling "Recycle" : "Win32: The object identifier does not representException calling "Recycle" : "Win32: The object identifier does not represent a valid object.

the function I was using to recycle the application pools was:

function recycle-pool($strServerName)
{
    $objWMI = [WmiSearcher] "Select * From IIsApplicationPool"
    $objWMI.Scope.Path = "\\" + $strServerName + "\root\microsoftiisv2"
    $objWMI.Scope.Options.Authentication = 6
    $pools = $objWMI.Get()
    foreach ($pool in $pools)
    {
        $pool.recycle()
        if (!$?)
        {
            Write-Host $pool.name " - ERROR"
        }
        else
        {
            Write-Host $pool.name " - Recycled"
        }
}

Any idea on what the problem is and how I should approach this?

回答1:

The original question was for IIS6, but I ran into something similar using the WebAdministration Module's Restart-WebAppPool on Windows 2012. So I dropped back to calling AppCMD, and that worked fine:

& $env:windir\system32\inetsrv\appcmd recycle apppool "YOURAPPPOOLNAMEHERE"

Sometimes, you don't have to over-engineer the solution. Hope that helps others some day.



回答2:

One of the application pools was stopped, which was causing the error. The other application pools were recycling fine. The code above is ok to use for anyone else.



回答3:

You can try to recycle with ADSI:

$server = "IIsServerName"  
$iis = [adsi]"IIS://$server/W3SVC/AppPools"  
$iis.psbase.children | foreach {  
    $pool = [adsi]($_.psbase.path)   
    $pool.psbase.invoke("recycle")  
}