Recycling Application Pools using WMI in IIS 6.0
Can one recycle an application from a script in IIS 6.0?
We (MS) certainly improved our story around ensuring reliablity and performance with the release of IIS 6.0. IIS 6.0 has had numerous positive comments around the process archictecture and also the server's ability to maintain it's own health. The building blocks of the reliability of IIS 6.0 is around the creation of this term called "Application Pools." Without sounding nuts, let me just phrase this as meaning "Application Pools are a single or collection of application chosen to be isolated based on application demands."
That is why we here so often at Microsoft that administrators are continually frustrated by the overall lack of guidance in the area of recycling. What are the guidelines for recycling? For example, is state (application or session) lost? Are connections reset or are friendly errors such as 503's returned?
To save time, check out this webcast we did on this topic for more information to answers relating to this question...
Recycling IIS 6.0 Applications: The Good, the Bad, and the Ugly
The powerful new architecture of IIS 6.0 relies on techniques such as recycling of worker processes for application reliability and availability. This webcast explains recycling and focuses on some of its potential pitfalls. Learn how to configure the ASP.NET session state service to maintain uptime while minimizing interruptions of application availability.
I seem to send the same code out that I swiped from a program manager and tester (Thanks Alexis and Bruno) here at Microsoft so I thought, hey, why not post it. This simple code if copied onto a IIS server will allow a user to recycle an application pool. To do so, you will simply need to paste this into a text file and save it as RecycleAppPool.js. To use it, simply type in the following:
RecycleAppPool.js <AppPoolNameString>
For example, to recycle the DefaultAppPool that is configured on Windows Server 2003 by default, type the following -
RecycleAppPool.js DefaultAppPool
// get app pool name
cmdArgs = WScript.Arguments;
var appPoolName = cmdArgs(0);
WScript.Echo("This application pool will be recycled: " + appPoolName);
// instantiate the IIS WMI provider
var service = GetObject( "winmgmts:/root/MicrosoftIISv2" );
var appPools = service.ExecQuery(
"SELECT * FROM IIsApplicationPool " +
" WHERE Name LIKE '%/" + appPoolName + "' "
);
var appPoolEnum = new Enumerator( appPools );
for( ; ! appPoolEnum.atEnd(); appPoolEnum.moveNext() )
{
appPoolEnum.item().Recycle;
}
After first releasing this, someone asked me if I might know of a mechanism to do this using WMI and managed code. After the help of Alexis, I have located this sample which should allow you to do the very same -
Instructions:
-
Save this to a file called recycle.cs
-
In the directory where this file is located, use the following command to compile as a .exe
%windir%\Microsoft.NET\Framework\v1.1.4322\csc.exe /r:System.dll /r:System.Management.dll /t:exe Recycle.cs
Now, type recycle.exe. Note, this is a very simple example that does not accept parameters like the example in classic JS script. Thus, it should require some minor modifications.
using System;
using System.Management;
namespace RecycleAppPool
{
class RecycleAppPool
{
static void Main(string[] args)
{
ManagementScope scope = new ManagementScope("root\\MicrosoftIISv2");
scope.Connect();
ManagementObject appPool = new ManagementObject(scope, new ManagementPath("IIsApplicationPool.Name='W3SVC/AppPools/DefaultAppPool'"), null);
appPool.InvokeMethod("Recycle", null, null);
Console.WriteLine("Recycled DefaultAppPool")
}
}
}
I hope that you find this useful and you are welcome to extend the code as you see fit to work in your organizations.
If you are interested in finding the exact application pool string name that is running currently on your systems, use the built-in application "iisapp.vbs" from the command line. It should return something like the following:
D:\WINDOWS>iisapp
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.W3WP.exe PID: 596 AppPoolId: DefaultAppPool
If you are one of those who has bit-the-bullet and put 2K3 Service Pack 1 on your machine, you will be happy to know we updated iisapp to now include recycling functionality. Hence, you don't need that script listed above...seriously, give it a shot!
D:\WINDOWS>iisapp /p 596 /r
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.Application pool 'DefaultAppPool' recycled successfully.
D:\WINDOWS>iisapp /a DefaultAppPool /r
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.Application pool 'DefaultAppPool' recycled successfully.
~Chris