title: Repeat a Powershell Script Untill done
body: <p>I am writing a Script, to restart multiple computers at once.</p>
  
  <p>I am trying to make it as human-like behavior, once computers are rebooted it will ping and check if computers are coming back, but I want it keeps pinging back the computers which didn't come up for 10 minutes and at last if not response it says that something went wrong with this computer and didn't come back. </p>
  
  <p>also if you see this line, I'm importing computers back again to test connecting after rebooting is it possible not to do so and tested connection only those computers which are rebooted.Thanks.</p>
  
  <pre><code> Start-Sleep -Seconds 120   
      $coms=GC c:\restarted.txt
  </code></pre>
  
  <p>complete script.</p>
  
  <pre><code>$computers=GC c:\list.txt
           ForEach ($computer in $computers)
  {if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)){
         Try {
  Restart-computer -ComputerName $computer -force -ErrorAction stop
  Write-Host "Restarting $computer" -f green
  $($computer)|Out-File c:\restarted.txt -append
  }
  Catch {
  [system.exception]
  Write-Warning "Failed to restart $($computer) `n$error[0]"
  }
  }
  else {
       Write-Host "cannot reach $computer and cannot restart" -BackgroundColor red
       }
       }  
  
       ## wait for 2 Minutes to check if the computer is coming back!
  Start-Sleep -Seconds 120   
  $coms=GC c:\restarted.txt
  Start-Sleep -Seconds 1
  ForEach ($com in $coms)
  {if(!(Test-Connection -Cn $com -BufferSize 16 -Count 1 -ea 0 -quiet))
          {write-host "$com still booting" -f red}
  
  else {
  
          Write-Host "$com is coming back !" -f Green
       }
       }
  </code></pre>