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 <> 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: " & Obj.Name
HandleError("Cannot recycle AppPool " & 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 '" & WMI_WA_NAMESPACE & "'")
Set waObj = objWMI.ExecQuery(WMI_WA_APPPOOLS)
HandleError("Error after execute WMI Query '" & WMI_WA_APPPOOLS)
For Each Obj in waObj
If Obj.GetState() = 1 Then
Obj.Recycle
HandleError("Cannot recycle AppPool " & Obj.Name)
WSCript.StdOut.WriteLine "AppPool was recycled: " & Obj.Name
Else
WSCript.StdOut.WriteLine "AppPool is not started and will not be recycled: " & 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 <> 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 <> 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 & VbCrLf & msg & VbCrLf
If UCase(Hex(Err.number)) = UCase("80041006") Then
msg = msg & "WMI Error. Not enough memory for the operation." & VbCrLf
End If
WScript.StdOut.WriteLine msg
msg = msg & " [" & ScriptInfo() & "]"
LogEvent msg, 1
End If
End Sub
Function ScriptInfo()
Dim commandLineInfo : commandLineInfo = WScript.ScriptFullName
Dim argument
For Each argument In WScript.Arguments
commandLineInfo = commandLineInfo & " """ & argument & """"
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