I have a batch script that allows me to turn off a site, deploy files and turn the site back on.

  1. Stop the application pool - works
  2. Stop the website - works
  3. Deploy files - works
  4. Start Application Pool - works only sometimes!
  5. Start the website - works if previous works

I'm running Windows Server 2012 R2, and the batch script is executed by an Octopus Deploy tentacle.

The line it is failing on is:

 Start-WebAppPool -Name $appPoolName

Where $appPoolName is live.website.com

This line works sometimes but not others, and is not consistent in any pattern.

I have the same script working on other servers. I have checked whether Application Information service is running and it is running fine. There are no system logs in the event viewer.

Although, I have this one application error which is raised when the Start-WebAppPool is called:

ERROR  + Start-WebAppPool -Name $appPoolName
ERROR  start-webitem : The service cannot accept control messages at this time. 

Do anyone know why this may be happening? I have tried to write a do-while loop until it is in a "Started" state, but it loops forever failing.


Turns out the process isn't stopping when I turn the Application pool off.

Why would the process continue to run after stopping the application pool? It literally continues running, without stopping.


So - following the comments below, when I stop the application pool I now make sure it is completely at stopped state before continuing the script.

This is the script I now have and is fully working:

# Load IIS module:
Import-Module WebAdministration

# Get AppPool Name
$appPoolName = $OctopusParameters['appPoolName']

if ( (Get-WebAppPoolState -Name $appPoolName).Value -eq "Stopped" )
    Write-Host "AppPool already stopped: " + $appPoolName
    Write-Host "Shutting down the AppPool: " + $appPoolName
    Write-Host (Get-WebAppPoolState $appPoolName).Value

# Signal to stop.
Stop-WebAppPool -Name $appPoolName

    Write-Host (Get-WebAppPoolState $appPoolName).Value
    Start-Sleep -Seconds 1
until ( (Get-WebAppPoolState -Name $appPoolName).Value -eq "Stopped" )
  • 1
    Sounds to me like you're issuing the App Pool stop command successfully, but it's not actually stopped by the time you try to start it again. Probably because the "process" that you mention in your edit is holding it in a running state (or perhaps in "stopping" state), waiting for something to finish. Is it always the same process holding it? What is that process? (System process, or part of your web app, or ???). If it's a process that apart of your web app, then why not debug it and figure out what it's waiting on (if anything)? – Ƭᴇcʜιᴇ007 Nov 9 '15 at 16:59
  • 1
    As a stop-gap, perhaps add code to your script to wait until the app pool is actually in a stopped state before continuing on in the script? – Ƭᴇcʜιᴇ007 Nov 9 '15 at 17:01
  • 2
    @Base33, can you paste the answer in an answer and mark is as the solution? Then this will no longer show up as "unanswered" – HackSlash Apr 19 '18 at 17:50

Octopus Deploy has some community PowerShell scripts, which you can find here https://library.octopus.com/listing

This is the content of one of them, which has retries:

# Load IIS module:
Import-Module WebAdministration

# Get AppPool Name
$appPoolName = $OctopusParameters['appPoolName']
# Get the number of retries
$retries = $OctopusParameters['appPoolCheckRetries']
# Get the number of attempts
$delay = $OctopusParameters['appPoolCheckDelay']

# Check if exists
if(Test-Path IIS:\AppPools\$appPoolName) {

    # Stop App Pool if not already stopped
    if ((Get-WebAppPoolState $appPoolName).Value -ne "Stopped") {
        Write-Output "Stopping IIS app pool $appPoolName"
        Stop-WebAppPool $appPoolName

        $state = (Get-WebAppPoolState $appPoolName).Value
        $counter = 1

        # Wait for the app pool to the "Stopped" before proceeding
            $state = (Get-WebAppPoolState $appPoolName).Value
            Write-Output "$counter/$retries Waiting for IIS app pool $appPoolName to shut down completely. Current status: $state"
            Start-Sleep -Milliseconds $delay
        while($state -ne "Stopped" -and $counter -le $retries)

        # Throw an error if the app pool is not stopped
        if($counter -gt $retries) {
            throw "Could not shut down IIS app pool $appPoolName. `nTry to increase the number of retries ($retries) or delay between attempts ($delay milliseconds)." }
    else {
        Write-Output "$appPoolName already Stopped"
else {
    Write-Output "IIS app pool $appPoolName doesn't exist"

Which comes from this library template https://library.octopus.com/step-templates/3aaf34a5-90eb-4ea1-95db-15ec93c1e54d/actiontemplate-iis-apppool-stop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.