Const commandLine = "%windir%\system32\inetsrv\appcmd.exe list apppools /xml"
Dim WShell : Set WShell = CreateObject("wscript.shell")
Dim oExec : Set oExec = WShell.Exec(commandLine)
If Not oExec.StdOut.AtEndOfStream Then
Dim stdOutput : stdOutput = "" & oExec.StdOut.ReadAll()
End If
Dim execResult : execResult = oExec.ExitCode
' Write error output and exit if result is not 0
If 0 <> execResult Then
Wscript.StdErr.Write stdOutput
Wscript.Quit execResult
Else
Dim objXMLDoc : Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.loadXml(stdOutput)
Dim xPath : xPath = "appcmd/APPPOOL"
Dim xmlNodes : Set xmlNodes = objXMLDoc.getElementsByTagName(xPath)
Dim apppool
For each apppool in xmlNodes
Dim appPoolState : appPoolState = UCase(apppool.Attributes.getNamedItem("state").text)
Dim appName : appName = apppool.Attributes.getNamedItem("APPPOOL.NAME").text
If(appPoolState = "STARTED") Then
Dim commandLineR : commandLineR = "%windir%\system32\inetsrv\appcmd.exe recycle apppool """ & appName & """"
Dim oExecR : Set oExecR = WShell.Exec(commandLineR)
Dim stdOutputR : stdOutputR = ""
If Not oExecR.StdOut.AtEndOfStream Then
stdOutputR = oExecR.StdOut.ReadAll()
End If
execResult = oExecR.ExitCode
' Write error output and exit if result is not 0
If 0 <> execResult Then
Wscript.StdErr.Write stdOutputR
Wscript.Quit execResult
Else
Wscript.StdOut.Write stdOutputR
End If
End If
Next
End If
Dim oAPI : Set oAPI = CreateObject("MOM.ScriptAPI")
Call oAPI.LogScriptEvent("ActivatedApmAgent.RecycleIIS.Task.vbs", 3201, 0, "Recycle Application Pools task done.")
Wscript.Quit execResult
On Error Resume Next
Wscript.StdErr.Write Err.Description
Wscript.Quit Err.Code