Start stopped application pools with AppCmd in a loop. In my routine, I occasionally have to start multiple website application pools when they are in a stopped state. On more than one web server. Being a lazy system administrator, I find it too much work to log on every server. Therefore I start those application pools in a loop. A condition for me to start application pools is that the application pool autostart parameter is set to true. This is because I set autostart to false when I disable hacked websites, and those application pools may not be started until all problems are resolved of course. To start application pools, I use the AppCmd command.

Remote AppCmd usage

Utilizing AppCmd with a CMD shell FOR loop, it is very easy to start all application pools matching this condition, on multiple web servers at once. All you need is a text file to list your server hostnames in.

Text file with server hostnames

We need a text file to work with. Create a text file called allservers.txt, and put all your servers in there. One server name (or hostname) per line:

We use CMD’s internal command FOR to run through this file, line by line in a loop.

AppCmd command to conditionally start all IIS application pools

Now we have our text file with our hostnames in place, we use CMD’s internal FOR command. Just type for /? for its help: Runs a specified command for each file in a set of files.

Precisely what we need, because a set of files may also be one file. In the help we find the syntax to use:

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]

If we combine this with AppCmd’s command to start application pools, we can create a single command that is executed for every webserver listed in allservers.txt.

To communicate with remote web servers, we use winrs and the parameter -r:value. Winrs opens a connection to the web server hostname used as value and executes the command between double quote signs ("").

Read this too:   System.Collections.Generic.KeyNotFoundException "The given key was not present in the dictionary" with MySQL Connector/NET and utf8mb4 character set

Put all on one line:

FOR /F %I IN (allservers.txt)
DO @winrs -r:%I "AppCmd list AppPool
 /autostart:true /xml
 | AppCmd start AppPool /in"

What the AppCmd command does is basically: output all application pools that are in state:stopped in XML format. The /in parameter in the second AppCmd command tells AppCmd to use the XML output as input.

Of course you can inverse the process to stop (an) application pool(s).

Recycle application pools

If you just want to recycle all application pools (appPools) that are in state:running, use:

FOR /F %I IN (all_webservers.txt)
DO @winrs -r:%I "AppCmd list AppPool
 /state:started /xml
 | AppCmd recycle AppPool /in"
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *