How to stop and start application pools using VBScript and ADSI in Windows Server 2003 and IIS 6.0. The following VBScript locates all application pools in a stopped state and tries to start them. It does so by querying IIS' Metabase for all AppPool objects and their .State property. If the state is not running, then the application pool is started.

The VBScript code below is pretty much self-explanatory. It iterates over the metabase AppPool objects, finds out their .State value and start an AppPool if the state isn’t running.

Option Explicit
' Walk through IIS AppPools and print their AppPoolState
' Possible values for AppPoolState:
' 1 = starting
' 2 = running
' 3 = stopping
' 4 = stopped
' any other value =unknown
Dim ServerName
If WScript.Arguments.Count <> 1 Then
        Wscript.Echo "No ServerName variable provided."
        ServerName = Wscript.Arguments(0)
End If

ServerName = Lcase(ServerName)

Dim objAppPools, objAppPool
Set objAppPools = GetObject("IIS://" & ServerName & "/W3SVC/AppPools")
For Each objAppPool in objAppPools
  Set objAppPool = GetObject("IIS://" & ServerName & "/W3SVC/AppPools/" & objAppPool.Name )
    If objAppPool.AppPoolState <> 2 Then
      Wscript.Echo objAppPool.Name & " is not running ok."
          WScript.Echo objAppPool.Name & ", AppPoolState: " & objAppPool.AppPoolState & _
                ", Win32Error: " & objAppPool.Win32Error & " ("& hex(objAppPool.Win32Error)&")"
          Wscript.Echo State2Desc(objAppPool.AppPoolState)
          If Err.Number = 0 Then
            Wscript.Echo objAppPool.Name & " started."
          End If
        End If

' support function to translate numerical states into readable strings
Function State2Desc(nState)
    Select Case nState
    Case 1
        State2Desc = "Starting"
    Case 2
        State2Desc = "Started"
    Case 3
        State2Desc = "Stopping"
    Case 4
        State2Desc = "Stopped"
    Case Else
        State2Desc = "Unknown state"
    End Select
End Function

Set objAppPool = Nothing
Set objAppPools = Nothing

Save the script as startAllAppPools.vbs and use it as follows:

cscript.exe /nologo startAllAppPool.vbs [server_name]

Substitute [server_name] with your webserver name.

Appcmd.exe or PowerShell equivalents

In IIS 7.0+ with appcmd.exe or PowerShell you can achieve something similar, see for example:

Start a single application pool using VBScript

A short script to start a single application pool is:

Option Explicit

If WScript.Arguments.Count <> 1 Then
        Wscript.Echo "No application pool name provided."
        Dim AppPool
        AppPool = Wscript.Arguments(0)
End If

' start an application pool
Sub start_app(applicationpool)
        iisObjectPath = ("IIS://" & ServerName & "/W3SVC/AppPools/" & applicationpool)
        Set iisObject = GetObject(iisObjectPath)
                If (Err.Number <> 0) Then
                        'ReportError ()
                        WScript.Echo "Error starting: " & ObjectPath
                        WScript.Quit (Err.Number)
                        WScript.Echo applicationpool & " started."
                End If
        Set iisObject = nothing
End Sub

Use this one like: cscript.exe /nologo startAppPool.vbs [appPool_name], and substitute "appPool_name" with the application pool you want to start.

Show 1 Comment

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *