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 AppPoolState property. If the state is not running, the application pool is started.
In IIS 6.0 you can walk through all IIS://server/W3SVC/AppPools objects with ADSI in VBScript, check each pool's AppPoolState and call .Start on every pool that isn't running (state 2). In IIS 7.0 and later, use appcmd.exe or PowerShell instead.
I originally wrote this post in 2011 for Windows Server 2003 and IIS 6.0, both long out of support. The ADSI/Metabase approach only works in IIS 6.0, or in later IIS versions with the IIS 6 Metabase Compatibility feature installed. For current IIS versions, see the appcmd.exe and PowerShell alternatives below.
Start all stopped application pools using VBScript
The VBScript code below is pretty much self-explanatory. It iterates over the Metabase AppPool objects, finds out their AppPoolState value and starts 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."
Wscript.Quit
Else
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)
objAppPool.Start
If Err.Number = 0 Then
Wscript.Echo objAppPool.Name & " started."
End If
End If
Next
' 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 startAllAppPools.vbs [server_name]
Substitute [server_name] with your web server name.
Need to stop or recycle an application pool in IIS 6.0 as well? See IIS 6.0: stop, start or recycle application pools.
Start a single application pool using VBScript
A short script to start a single application pool on the local server is:
Option Explicit
' the server to connect to, use localhost for the local server
Const ServerName = "localhost"
If WScript.Arguments.Count <> 1 Then
Wscript.Echo "No application pool name provided."
Wscript.Quit
Else
Dim AppPool
AppPool = Wscript.Arguments(0)
start_app(AppPool)
End If
' start an application pool
Sub start_app(applicationpool)
Dim iisObjectPath, iisObject
iisObjectPath = ("IIS://" & ServerName & "/W3SVC/AppPools/" & applicationpool)
Set iisObject = GetObject(iisObjectPath)
On Error Resume Next
iisObject.Start
If (Err.Number <> 0) Then
'ReportError ()
WScript.Echo "Error starting: " & iisObjectPath
WScript.Quit (Err.Number)
Else
WScript.Echo applicationpool & " started."
End If
Set iisObject = nothing
End Sub
Save it as startAppPool.vbs and use it like cscript.exe /nologo startAppPool.vbs [appPool_name], substituting "appPool_name" with the application pool you want to start.
Appcmd.exe or PowerShell equivalents
In IIS 7.0+ you can achieve something similar with appcmd.exe or PowerShell, see for example:
- Conditionally start Application Pools on remote IIS web servers: start all stopped application pools that have Autostart set to true using PowerShell
- Set IIS Application Pool recycle defaults to Specific Times, not Regular Time Interval
- Recycle applicatiepools met PowerShell als deze een bepaalde scriptProcessor gebruikt (filter on a site's IIS scriptProcessor, sweet! 🙂 - in Dutch)