Hi,
recently I have written a little tool for controlling the application pools of an IIS webserver. My applications are being hosted on Windows 2003 Server and every virtual directory or application has its own application pool. Sometimes, especially when new versions of the applications get deployed automatically by the build server, specific application pools need to be recycled. As I did not find a simple tool on the web (except some strange vb scripts) to control my application pools, I decided to write a simple command line utility to do the job. Another reason was, that I wanted to be able to do it from .net directly.
So go ahead and check out the source I have attached to this post.
AppPoolCtl Source Code
The Code is licensed under the LPGL license.
The usage is very simple. Just call the program without any parameter and you will get the explanation.
So what is it actually doing? Lets have a quick look a the important lines of code in Program.cs which actually do the application pool reset:
37 static int Main(string[] args)
38 {
39 if(args.Length!=3)
40 {
41 Console.WriteLine("Usage: AppPoolCtl <servername> <applicationPool> <command>");
42 Console.WriteLine("Valid commands: Start, Stop, Recycle");
43 return -1;
44 }
45 string path = String.Format("IIS://{0}/W3SVC/AppPools/{1}", args[0], args[1]);
46 try
47 {
48 DirectoryEntry entry = new DirectoryEntry(path);
49 entry.Invoke(args[2], null);
50 Console.WriteLine("Command successful");
51 return 0;
52 }
53 catch (Exception ex)
54 {
55 Console.WriteLine("Error: ", ex.Message);
56 return 1;
57 }
58 }
First look at line 45 where I set up the Active Directory URL to get access to the IIS aplication pools. One important thing to mention here is that the user who executes the utility must have the administrative rights to do application pool resets on the target system. Normally this would be the administrator account itself or a member of the administrators group.
Finally in line 48 and 49 I open a DirectoryEntry to the path built in the line above and execute the proper command. It is that simple but to get this info I had to search in google for a while. So here it is – the AppPoolCtl command line utility! Have fun.
About me