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 property 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.

In this article you'll learn how to start all stopped application pools that have the ApplicationPool.AutoStart property set to $True using AppCmd.exe or PowerShell. This is something you often or occasionally have to perform on various servers, for example when an application pool hangs.

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.

AppCmd supports the piping of commands. You can use the /xml parameter to generate XML formatted output, and pipe it through to the /in parameter. It reads and operates on the XML input.

This makes it ideal to pipe multiple Appcmd commands into a single command, for example to start all application pools that are in a stopped state but having the AutoStart property.

C:\windows\system32\inetsrv\appcmd.exe list apppools /state:stopped /autostart:true /xml | appcmd start apppool /in

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

srv1.example.com
srv2.example.com
srv3.example.com
srv1.example.net
[...]
srv201.example.net

see my post Use -SearchBase with Get-ADComputer for faster results on how to get a list of all enabled web servers in your AD domain quickly.

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 ("").

Put all on one line:

FOR /F %I IN (allservers.txt)
DO @winrs -r:%I "AppCmd list AppPool
/state:stopped
/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).

want to reset the application pool recycle regular time interval?

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"

Start all stopped application pools that have Autostart set to $True using PowerShell

Start all stopped application pools in IIS that have the AutoStart property set to true. Easily with appcmd or the IISAppPool cmdlet that's available in the IISAdministration PowerShell module.

Here is an example for using Start-WebAppPool with Get-ChildItem to start all application pools with state "Stopped" (WebAdministration PowerShell module):

# if required: Import-Module WebAdministration
Get-ChildItem IIS:\AppPools | Where-Object { $_.autoStart -eq "True" -and $_.state -eq "Stopped" } | Start-WebAppPool

And using IISAdministration module:

Get-IISAppPool | ? { ($_.State -eq "Stopped") -And ($_.AutoStart -eq "True") } | %{ $_.Start() }

Using PowerShell Remoting

You can use Invoke-Command -ComputerName to start a session on a remote IIS web server, and use a ScriptBlock to start - or recycle for that matter - application pools:

Invoke-Command -ComputerName webserver -ScriptBlock {
Get-IISAppPool | ? {
($_.State -eq "Stopped") -And ($_.AutoStart -eq "True")
} | %{ $_.Start() }
}

Using remoting in PowerShell, you can easily start application pools on remote web servers using conditions. Like being stopped and having the AutoStart property set to true. Use Get-ADComputer to loop through your domain computer objects.

Donate a cup of coffee
Donate a cup of coffee

Thank you very much! <3 ❤️

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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