System Center Recycle Application Pools

AVIcode.DotNet.SystemCenter.RecycleAppPools (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
RecycleWA WriteAction Microsoft.Windows.ScriptWriteAction Default

Source Code:

<WriteActionModuleType ID="AVIcode.DotNet.SystemCenter.RecycleAppPools" Accessibility="Public">
<Configuration/>
<OverrideableParameters/>
<ModuleImplementation>
<Composite>
<MemberModules>
<WriteAction ID="RecycleWA" TypeID="Windows!Microsoft.Windows.ScriptWriteAction">
<ScriptName>RecycleAppPools.vbs</ScriptName>
<Arguments/>
<ScriptBody><Script>
On Error Resume Next

SetLocale("en-us")
HandleErrorContinue("Cannot set en-us locale")

Dim WShell : Set WShell = CreateObject("wscript.shell")
Call HandleError("Cannot create object WScript.Shell")

Const WMI_WA_NAMESPACE = "winmgmts:{impersonationLevel=impersonate}!\\.\root\webadministration"
Const WMI_WA_APPPOOLS = "SELECT * from ApplicationPool"

if IsIIS7() Then
RecycleApplicationAppPoolIIS7()
Else
Dim canRecycle : canRecycle = CanRecycleIIS6()
If (canRecycle = "1") Then
RecycleAppPoolIIS6()
Else
PerformRestartIIS()
End If
End If

Function PerformRestartIIS()
On Error Resume Next
WShell.run "iisreset", 0, true
End Function

Function CanRecycleIIS6()
On Error Resume Next
' check if IIS is not is IIs5 isolation mode
Dim IISObj : Set IISObj = getobject("IIS://Localhost/W3SVC")
HandleError("Cannot get via ADSI IIS://Localhost/W3SVC")

Dim IIs6Mode : IIs6Mode = IISObj.GetCurrentMode
If (Err &lt;&gt; 0) Then
CanRecycleIIS6 = 0
Err.Clear()
Else
CanRecycleIIS6 = IIs6Mode
End If
End Function

Function RecycleAppPoolIIS6()
On Error Resume Next
Dim IISObj : Set IISObj = getobject("IIS://Localhost/W3SVC/AppPools")
HandleError("Cannot get AppPool object IIS://Localhost/W3SVC/AppPools")

Dim Obj
For Each Obj in IISObj
Obj.Recycle
WSCript.StdOut.WriteLine "AppPool was recycled: " &amp; Obj.Name
HandleError("Cannot recycle AppPool " &amp; Obj.Name)
Next
End Function

Function RecycleApplicationAppPoolIIS7()
On Error Resume Next
Dim objWMI, waObj, Obj
Set objWMI = GetObject(WMI_WA_NAMESPACE)
HandleError("Failed to get WMINameSpace '" &amp; WMI_WA_NAMESPACE &amp; "'")

Set waObj = objWMI.ExecQuery(WMI_WA_APPPOOLS)
HandleError("Error after execute WMI Query '" &amp; WMI_WA_APPPOOLS)

For Each Obj in waObj
If Obj.GetState() = 1 Then
Obj.Recycle
HandleError("Cannot recycle AppPool " &amp; Obj.Name)
WSCript.StdOut.WriteLine "AppPool was recycled: " &amp; Obj.Name
Else
WSCript.StdOut.WriteLine "AppPool is not started and will not be recycled: " &amp; Obj.Name
End If
Next
End Function

Function IsIIS7()
On Error Resume Next
IsIIS7 = False

Const iisVersionKey = "HKLM\SOFTWARE\Microsoft\InetStp\MajorVersion"
Dim s1 : s1 = WShell.RegRead(iisVersionKey)
If Err &lt;&gt; 0 Then
' 0x80070003 - The system cannot find the path specified.
' 0x80070002 - The system cannot find the file specified.
If UCase(Hex(Err.number)) = UCase("80070003") Or UCase(Hex(Err.number)) = UCase("80070002") Then
Err.Clear()
Exit Function
Else
HandleError("Failed to get HKLM\SOFTWARE\Microsoft\InetStp\MajorVersion")
End If
End If

If NOT IsEmpty(s1) Then
If s1 = "7" Then
'check that ASP.NET component installed
Const aspNetKey = "HKLM\SOFTWARE\Microsoft\InetStp\Components\ASPNET"
Dim s2 : s2 = WShell.RegRead(aspNetKey)
If Err &lt;&gt; 0 Then
' 0x80070003 - The system cannot find the path specified.
' 0x80070002 - The system cannot find the file specified.
If UCase(Hex(Err.number)) = UCase("80070003") Or UCase(Hex(Err.number)) = UCase("80070002") Then
Err.Clear()
Exit Function
Else
HandleError("Failed to get HKLM\SOFTWARE\Microsoft\InetStp\Components\ASPNET")
End If
End If
If NOT IsEmpty(s2) Then
If s2 = "1" Then
IsIIS7 = True
End If
End If
End If
End If
End Function

Sub HandleError(customMessage)
If Not (Err.number = 0) Then
LogError customMessage
Wscript.Quit 0
End If
End Sub

Function HandleErrorContinue(customMessage)
HandleErrorContinue = False
If Not (Err.number = 0) Then
LogError customMessage
Err.Clear
HandleErrorContinue = True
End If
End Function

Sub LogError(customMessage)
Dim msg
If Not (Err.number = 0) Then
msg = Replace(" Error: #P1# Description: #P2# ", "#P1#", CStr(Err.number) )
msg = Replace(msg, "#P2#", Err.Description )
msg = customMessage &amp; VbCrLf &amp; msg &amp; VbCrLf
If UCase(Hex(Err.number)) = UCase("80041006") Then
msg = msg &amp; "WMI Error. Not enough memory for the operation." &amp; VbCrLf
End If
WScript.StdOut.WriteLine msg
msg = msg &amp; " [" &amp; ScriptInfo() &amp; "]"
LogEvent msg, 1
End If
End Sub

Function ScriptInfo()
Dim commandLineInfo : commandLineInfo = WScript.ScriptFullName
Dim argument
For Each argument In WScript.Arguments
commandLineInfo = commandLineInfo &amp; " """ &amp; argument &amp; """"
Next
ScriptInfo = commandLineInfo
End Function

Sub LogEvent (message, eventType)
Dim errorEventId : errorEventId = 20000
Dim oAPI0 : Set oAPI0 = CreateObject("MOM.ScriptAPI")
Call oAPI0.LogScriptEvent("AVIcode .NET Enterprise Management Pack for OpsMgr 2007", errorEventId, eventType, message)
End Sub

</Script></ScriptBody>
<TimeoutSeconds>600</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="RecycleWA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>