title: Leave if loop inside while loop
body: <p>I'm writing a PowerShell script that is constantly checking the status of a game server that I'm running and, depending on the states of certain exe's and variables, certain things will happen.  The way I have this constructed is, first I have a timer start and I have a while loop that runs up until the timer hits 280 seconds. Within this while loop there are a series of if than else statements that look at all of the aforementioned variable and states of exe's and then they execute different things.  What I'd like is that when it gets to the end of a condition (if the exe is not found and the server status is 0), then email me about that and then go back to the beginning of the entire <code>while</code> loop and keep looping and checking the status of the server.</p>
  
  <p>Here is the script right now in:</p>
  
  <pre><code>//$serverStatus = 1 means servers up, 0 means down, 3 means crashed
  $timeout = new-timespan -Seconds 280
  $sw = [diagnostics.stopwatch]::StartNew()
  while ($sw.elapsed -lt $timeout){
      Start-Sleep -Seconds 10
  
      if (((get-process $process -ea SilentlyContinue) -eq $Null) -And ($serverStatus -eq "1")) {
          #Server is down and email
          "0" &gt; "D:\Ark Scripts\serverStatus.txt"
          Write-Host "it's down - email"
          sendMail -sendTo "my email" -emailBody "Ark Server is down."
  
      } elseif (((get-process $process -ea SilentlyContinue) -eq $Null) -And ($serverStatus -eq "0")) {
          #Server is down and we are aware
          Write-Host "it's down - we know"
      } elseIf (((get-process $process -ea SilentlyContinue) -ne $Null) -And ($serverStatus -eq "0")) {
          #Server is up and email
          "1" &gt; "D:\Ark Scripts\serverStatus.txt"
          Write-Host "it's up - email"
          sendMail -sendTo "my email" -emailBody "Ark Server is back up."
      } elseif ($serverStatus -eq "3") {
          Send-RCON "listplayers" &gt; "D:\Ark Scripts\RCONlog.txt"
  
          $log = Get-Content "D:\Ark Scripts\RCONlog.txt"
  
          if ($log -eq "Authentication failed!") {
              Write-Host "crashed and we know"
          } else {
             "1" &gt; "D:\Ark Scripts\serverStatus.txt"
             Write-Host "Back up"
          }
      } else {
          #Checks RCON connection for crashes and sends crash email if connection fails.
          Send-RCON "listplayers" &gt; "D:\Ark Scripts\RCONlog.txt"
  
          $log = Get-Content "D:\Ark Scripts\RCONlog.txt"
  
          if ($log -eq "Authentication failed!") {
              Write-Host "crashed"
              "3" &gt; "D:\Ark Scripts\serverStatus.txt"
              sendMail -sendTo "my email" -emailBody "Ark Server has crashed."
              return
          } else {
              "1" &gt; "D:\Ark Scripts\serverStatus.txt"
              Write-Host "it's up"
          }
      }
  }
  
  Exit-PSSession
  </code></pre>
  
  <p>I just want this while loop to keep running every 10 seconds to check the state of the variables and whether the exe is found and constantly cycle.</p>