0

I have been working on this PowerShell script. I'm still pretty new at this. The way it works is that I give it a list of servers which it goes though and restarts the App Pools 1 by 1. I have been having a problem with the snippet below. I do not use Restart-WebAppPool because it sometimes gives the App Pool a start before it's ready and leaves it stopped. I cannot show the whole script because it's big and has proprietary info. The problem that I'm having is I can't seem to break the do-while loop. In it I'm checking App Pool status to make sure that it's stopped.

What I get appears for $PL_Break appears to be a valid string showing "Stopped". However, even when it shows "Stopped" it doesn't break the loop.

$PL_Timeout = New-TimeSpan -Seconds 95

foreach ($PL_Server in $PL_ServerName) { $PL_Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

Write-host "`n`n`nRestarting App Pool : $PL_AppPool"
write-host "Stopping: " $PL_Server -f Green

Invoke-Command -ComputerName $PL_Server -ArgumentList $PL_AppPool -ScriptBlock {param($PL_App) Stop-WebAppPool -Name $PL_App} 

do {

sleep 5
$PL_Br = Invoke-Command -ComputerName $PL_Server -ArgumentList $PL_AppPool -ScriptBlock {param($PL_App) Get-IISAppPool $PL_App | Select-Object State}

$PL_Break = [string]$PL_Br.State.value

} while (($PL_Stopwatch.elapsed -lt $PL_Timeout) -or ($PL_Break -ne "Stopped"))

} # Foreach - Server

11
  • 1
    you're looking for -and instead of -or - $PL_Stopwatch and $PL_Timeout are not defined in your code by the way, I hope they're in your actual script. Commented Apr 27, 2022 at 22:19
  • I'm using or on purpose because I want it to break if it reaches the timeout or the App Pool successfully stops. I've used this specific type of loop in other scripts. I do have the timeout defined earlier in the script. This specific case is giving me trouble because when the timeout hasn't yet run out and the App Pool successfully stops, I get a string for $PL_Break that indicates it's stopped, but the loop doesn't break. Commented Apr 27, 2022 at 22:39
  • 1
    read it in English, I think you're misunderstanding your loop breaking condition... you want to loop while the timer is below the timeout OR the variable is not equal to stopped or while the timer is below the timeout AND the variable is not equal to stopped ? Commented Apr 27, 2022 at 22:43
  • 1
    but you are saying to keep running the loop while EITHER of those are true Commented Apr 27, 2022 at 22:48
  • 1
    AAHH... lightbulb! Thanks I see what you are saying. Thanks! Commented Apr 27, 2022 at 22:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.