IIS Recycling Application Pools

October 3, 2014

So I wrote a little VBScript to help recycle all the application pools on a single server. This will by far be the fastest script you’ll find out here.

VBScript

on error resume next
Set args=wScript.Arguments
If args.count<>1 Then
 wScript.echo WScript.ScriptName & " SERVERNAME"
 wScript.Quit
Else
 s=args(0)
End If
Set objWMIService = GetObject("winmgmts:{authenticationLevel=pktPrivacy}\\" & s & "\root\microsoftiisv2")
Set colItems = objWMIService.ExecQuery("Select * From IIsApplicationPool Where Name LIKE 'W3SVC/AppPools/%'")
For Each objItem in colItems
 wscript.echo objItem.Name & ",recycled"
 objItem.Recycle
 If err<>0 then
 wScript.echo err.Description
 err.clear
 End If
Next

Usage:

Recycle-all-appPools.vbs SERVERNAME

Why is this script faster than others?

Using WMI and specifically selecting the data you want is much faster than loading all of the data that isn’t really important to you, especially if all you plan on doing is invoking the Recycle command.

Powershell

And because PowerShell has made everything easier, here is a quick snippet that you can use to easily do the same thing, for IIS 7-8.5
Invoke-Command -ComputerName SERVER1,SERVER2 {Restart-WebAppPool -Name *}

You can also get-contents (gc) of a file and pipe it to Invoke-Command (icm) to do multiple hosts, for example:

say I have this file c:\hosts.txt

c:\hosts.txt
server1
server2
server3

gc c:\hosts.txt|icm {Restart-WebAppPool -Name *}

that will recycle all of the AppPools on each of the servers in c:\hosts.txt.

+1 this post if it helped you!

Leave a Reply