Conditionally start Application Pools on remote IIS web servers

Home » Windows Server » Conditionally start Application Pools on remote IIS web servers

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.

In my routine, I occasionally have to conditionally start application pools in IIS 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 and use a mix of AppCmd.exe and PowerShell.

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. Those application pools may not be started until all problems are resolved, of course.

Remote AppCmd usage

Utilizing AppCmd.exe 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.exe 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.exe 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.exe list AppPool /state:stopped /autostart:true /xml | AppCmd.exe 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).

Also read:

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.

Remove Autostart property from DefaultAppPool

Here is how to remove the autoStart property using PowerShell (one of many possible methods):

Get-IISAppPool | ? { ($_.State -eq "Started") } | % {
  if ($_.Name -eq "DefaultAppPool") {
    if ($_.Autostart -eq "True") {
      Start-IISCommitDelay # for objects 
      $_.Autostart = 'False'
      Stop-IISCommitDelay
    }
  }
}

For objects returned by Get-IISAppPool you have to call Start-IISCommitDelay prior to changing the values and then Stop-IISCommitDelay after making the changes. You see Start-IISCommitDelay and Stop-IISCommitDelay in action in my post IIS 10.0 FTP IP Security allow list.

One-time donation

Please take a second to support Sysadmins of the North and donate, your generosity helps!

Conclusion starting IIS application pools conditionally

In this article you learned how to start all stopped application pools that have the ApplicationPool.AutoStart property set to $True. You perform this action either 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.

Summary

  • You can conditionally start application pools in IIS that are stopped and have the AutoStart property set to true.
  • Use AppCmd.exe with a CMD FOR loop to start application pools on multiple web servers efficiently.
  • A text file with server hostnames allows you to execute commands remotely using winrs.
  • PowerShell can also help start application pools conditionally using cmdlets like Start-WebAppPool or Invoke-Command.
  • This article provides methods to handle application pool management effectively across various servers.
Jan Reilink
Jan Reilink

In my day to day work, I’m a systems administrator – DevOps / SRE and applications manager at Embrace – The Human Cloud. At Embrace we develop, maintain and host social intranets for our clients. Provide digital services and make working more efficient within various sectors.

Want to support me and donate? Use this link: https://www.paypal.com/paypalme/jreilink.

Articles: 158