IIS Appcmd - All things CLI

Past couple of days I was digging into Appcmd.exe - the command line tool for managing IIS 7 and 7.5 for an on-going project. If you are not yet aware of this tool yet, surprisingly you will not be the only one as somehow appcmd.exe hasn't been put to limits by a lot of IIS admins yet ...
Appcmd is not only a useful tool for odd daily IIS tasks but its an excellent tool for automation. Commands in appcmd are pretty clear, straight-forward and easy to understand that a person is limited or no experience with scripting or automation can easily start working on it with great results.

A lot of people I have spoken to about this tool treat it as a substitue for adsutil.vbs in IIS 6. I must say this tool is much more than just a substitute. The scenario I was working on, I had to enumerate all the application pool names on 250 web servers to make a report on the app pool usage analysis. At the surface this seems a pretty heavy task. Here's what I did:

getcommand.bat
This was my simple old school windows batch file that had commands that I actually needed to execute on the remote server to retrieve the application pool names. The contents of the .bat file were:

@ECHO OFF
hostname
c:\windows\system32\inetsrv\appcmd.exe list apppool /text:name

The bacth file couldnt have been simpler than that. By default "list apppool" command will enumerate the application pool names along with their current state, the .Net framework associated to them and few other properties. In my case I din't need those. Including /text:name will only output the name of the application pool.

Note: Running the above command with /text:* will give you detailed information on the application pool, inclusive of the application pool password in plain text which is not available through GUI. It s good way to figure out in case you have any password issues going on, which in my case are abundant. :-)

If you have any other method of running scripts or batch files in your environment, the above is all you need to get your work done.

What I am doing below is integrating the batch file with a utility named psexec.exe from Sysinternals to run the above batch file remotely on the 200 odd servers I needed the data from.

serverlist.txt
This is a simple text file which contains server names on which we are planning to run our commands. You will have to place 1 servername on each line.

psexec.exe
This tool is free and included in the Sysinternal Suite. PSEXEC helps you run command on remote machines.

apppool_names.txt
This is going to be our output text file for collecting results.

The last thing you need are Administrative priviliges on every machine you are planning to run this command on.

Here's how you implement it:

Open and command prompt with elevated priviliges and follow the below syntax:

psexec.exe @serverlist -c commandtorun >>output

in place of the file names its always good to place the complete path to the file. so in my case the command was:

psexec.exe @c:\serverlist.txt -c c:\getcommand.bat >>c:\apppool_names.txt

the -c in the above command will actually copy the batch file to every server and then execute it locally. You can also run just the appcmd command listed in getcommand.bat with psexec but then it will make things messy in the output because of which I made a batch file and included the hostname command to get the respective servername into the output...

What would you like to automate on IIS?

Related Resources:
How To: AppCMD in IIS 7 - An Introduction
How To: Using IIS 6.0 Command Line Utilities - Part 1
How To: Using IIS 6.0 Command Line Utilities - Part 2
WMI Integration in Batch File Scripting


Hope this Helps.

No Comments