title: Powershell Loop and While Optimization
body: <p>I have created a script to restart on a predetermined time period.  Unfortunately, while it's checking the time I run into cpu usage issues.  It just runs and runs (Suppose this is the essence of a while loop).  I'm looking for a better way to do this.  </p>
  
  <p>So basically I have it setup to have times that it looks for, for example: 12:00 </p>
  
  <p>I then run a while and it keeps running until it hits 12:00 and then executes some things and runs again to the next time interval. </p>
  
  <p>Interestingly enough it escaped my mind while creating this that it would be a resource hog and extremely inefficient.  </p>
  
  <p>So my question presents itself as to what should I do if I want to restart something on certain intervals, but not want it to check every second/ millisecond.  </p>
  
  <p>This would be restarting a gameserver process and the timing is critical because people need warning in game.  </p>
  
  <p>I will include the script for viability.</p>
  
  <pre><code>$CurrentTime = Get-Date -UFormat "%R"
  
  $debug = "true" 
  
  $Times = "12:00", "13:00", "01:00", "14:12"
  $servers = "Notepad"
  $aff = "C4", "38"  
  
  $scriptRun = 0
  $serverCount = 1
  
  $startedServers
  
  $loop = 1
  
  function checkServers 
  {
      if ($debug -eq "true") { Write-Output "Entering Loop ::: $loop" }
      while($loop -eq 1) 
      {
  
      if ($CurrentTime -eq $Times[1] -or $CurrentTime -eq $Times[2] -or $CurrentTime -eq $Times[3] -or $CurrentTime -eq $Times[4]) 
      {
          stopServers
          foreach($i in $servers)
          {
              if ($debug -eq "true") { Write-Output "The Server being started is $server" }
              startServer( [ref]$servers[$i] )
          }
          $loop = 0
      } else {
          $loop = 1
          checkServers
      }
      }
  }
  
  function waits 
  {
      Start-Sleep -seconds 61
      $loop = 1
      checkServers
  }
  
  function stopServers 
  {
      if ($scriptRun -eq 1)
      {
          foreach ($i in $startedServers)
          {
              Stop-Process -processname $startedServers[$i]
              Write-Output "$startedServers[$i] has been stopped"  
          }
      } else {
      Write-Output "Starting Servers for the first time"
      }   
  }
  
  function startServer( [ref]$server ) 
  {
      $server = $server 
  
      Start $server 
      $s = "$server.exe"
      $instances = Get-Process $s
  
      foreach ($s in $instances) {
          $s.ProcessAffinity = $aff[$serverCount]
          $serverCount = $serverCount + 1 
      }
  
      if ($scriptRun -eq 0) {
          $startedServers += $s
          $scriptRun = 1 
      }
      waits
      Write-Output "$server was successfully started at $CurrentTime" 
  }
  
  Write-Output "Starting RestartServer at $CurrentTime"
  checkServers
  </code></pre>
  
  <p>**Yes there are probably other errors and I am working through the most obvious one first then will get to the others.  </p>