Restarting an IIS6 app pools using .NET
This week I was writing an admin tool that needed to recycle some IIS6 application pools. I found two ways to do it, one using Window Management Instrumentation (WMI) and the other using the DirectoryEntry class (part of the active directory classes).
Here’s a little code sample for the WMI way:
-
ManagementScope scope = new ManagementScope(@”\\localhost\root\MicrosoftIISv2");
-
WqlObjectQuery query =
-
new WqlObjectQuery(@”SELECT * FROM IIsApplicationPool WHERE Name LIKE '% .NET2'");
-
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
-
foreach (ManagementObject appPool in searcher.Get())
-
{
-
appPool.InvokeMethod("Recycle", null);
-
}
Here’s a code sample for the DirectoryEntry way:
-
DirectoryEntry appPools =
-
new DirectoryEntry("IIS://localHost/w3svc/AppPools", "", "");
-
foreach (DirectoryEntry child in appPools.Children)
-
{
-
if (child.Name.EndsWith(".NET2"))
-
{
-
child.Invoke("Recycle", null);
-
}
-
}
There are some more great examples of how to do these types of things on stack overflow.







Comments