Home All Groups Group Topic Archive Search About

Recycle remote II6 application pools using WMI \ Powershell



Author
2 Nov 2007 10:01 PM
Bill
I'm having some trouble finding the best method for recycling remote IIS6
application pools using PowerShell.

In the past this has always been very easy using VB script:

Function PoolRecycle(strServer)

    On Error Resume Next
    Err.Clear

    Dim objWMIService
    Dim colItems
    Dim objItem

    'recycle application pool
    WScript.Echo ""
    Err.Clear
    Set objWMIService = GetObject _
           ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
                   & strServer & "\root\microsoftiisv2")

    'connect to WMI
    Set colItems = objWMIService.ExecQuery("Select * From IIsApplicationPool")

    'Step through the objects to be recycled
    For each objItem in colItems
        objItem.Recycle
        If err = 0 Then
            WScript.Echo(strServer & " - " & ObjItem.Name & " Recycled")
            PoolRecycle = "success"
        Else
            WScript.Echo("Unable to recycle the application pool on " & strServer)
            PoolRecycle = "failed"
        End If
    Next       

End Function

I have attempted to duplicate the same thing in PowerShell, but I believe I
am still running into an issue with the pktPrivacy authentication setting. 
The following code works when executing localy, but not on a remote host:

Function PoolRecycle($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 help or better ways of accomplishing this in PowerShell would be very
much appreciated!

Thanks
Bill

AddThis Social Bookmark Button