Use passthru to make sure the cmdlet is finished
-Passthru
is used to pass current object (i.e. AppPool) further down the pipeline. I don't think that it's guaranteed, that Start/Stop-WebAppPool
cmdlets will wait for current operation to complete before passing down AppPool object. It works now, but it could break later.
Solution? Use Restart-WebAppPool. Or if you really need to use Start/Stop cmdlets, do it like this:
$AppPool = 'Foo'Stop-WebAppPool -Name $AppPool while((Get-WebAppPoolState -Name $AppPool).Value -ne 'Stopped'){ Start-Sleep 1}Start-WebAppPool -Name $AppPool