Trying to start app pool via Powershell Script - getting error intermittently

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.

Update

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.

Fixed!

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
}
else
{
    Write-Host "Shutting down the AppPool: " + $appPoolName
    Write-Host (Get-WebAppPoolState $appPoolName).Value

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

do
{
    Write-Host (Get-WebAppPoolState $appPoolName).Value
    Start-Sleep -Seconds 1
}
until ( (Get-WebAppPoolState -Name $appPoolName).Value -eq "Stopped" )

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
        do{
            $state = (Get-WebAppPoolState $appPoolName).Value
            Write-Output "$counter/$retries Waiting for IIS app pool $appPoolName to shut down completely. Current status: $state"
            $counter++
            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