title: Continuously check if website is available with Powershell
body: <p>I have a server running a website that for some reason just drops dead whenever it feels like it. Not sure why and i know this solution I´m going to describe is just a way of putting out the fire and not solving the problem. But for now that´s ok.</p>
  
  <p>So, i want to run a Powershellscript on the server that continuosly checks if the website is available or not and based on that perform some magic to get it running again.</p>
  
  <p>So far i have this code.</p>
  
  <p>When i turn on the Apache and runs the script, the function "running" is working.
  When i turn off the Apache and runs the script, the function "notrunning" is working.</p>
  
  <p>But the problem is, when i start the Apache, run the script and "running" is continuosly looping and i turn of the Apache (script still running) i want it to go on to "notrunning", fix the problem, start my service again and check from the start if the site is up and go to "running". But it get stuck on "running" and wont change to "notrunning".</p>
  
  <p>Any idea how i can make this happen?</p>
  
  <p>(I edited the script to take out the real process and service name for my companys sake)</p>
  
  <pre><code>function checkSite
  {
  [string] $url = 'http://websitegoeshere.com'
  [net.httpWebRequest] $req = [net.webRequest]::create($url)
  $req.Method = "HEAD"
  [net.httpWebResponse] $res = $req.getResponse()
  while ($res.StatusCode -ge "200") {running} notrunning
  }
  
  function running
  {
  cls;
  Write-Host "  -=IS-checker=-" -fore yellow
  Write-Host "" -fore green
  Write-Host "----------------------" -fore green
  Write-Host " Access to web = OK " -fore green
  Write-Host "----------------------" -fore green
  sleep 60
  }
  function notrunning
  {
  cls;
  Write-Host "-=IS-checker=-" -fore yellow
  Write-Host "" -fore red
  Write-Host "-------------------------" -fore red
  Write-Host "  Site down - Site Down  " -fore red
  Write-Host "-------------------------" -fore red
  sleep 2
  $process = "process-name-goes-here"
  Write-Host "Stopping process" $process -fore red 
  Stop-Process -processname $process
  sleep 5
  $service = "service-name-goes-here"
  Write-Host "Killing service" $service -fore red
  Stop-Service $service -Force
  sleep 5
  Write-Host "Starting service" $service -fore green
  Start-Service $service
  sleep 5
  $LogTime = Get-Date -Format "yyyy-MM-dd hh-mm-ss"
  $LogFile = 'C:\PSLog\'+"LOG_"+$LogTime+".log"
  $LogTime+" Stopped "+$process+", Stopped "+$service+", Started "+$service | Out-File $LogFile
  Write-Host "Logfile written to"$LogFile -fore green
  sleep 5
  checkSite
  }
  </code></pre>
  
  <p>checkSite</p>