title: How to recycle appool when IIS getting slow response by using Powershell?
body: <p>I am trying to write a script to check health of my web application's health status. Forexample if I couldn't get message in 10 seconds I have to recycle my apppool by using Powershell.Or except 200-ok codes, my app pool should recycled.</p>
  
  <p>Please look at below code and ERROR:</p>
  
  <pre class="lang-py prettyprint-override"><code># Load IIS module:
  Import-Module WebAdministration
  while ($true) {
      write-host 'Runnig For Check app.xxx.com ...'
  
  # First we create the request.
  $HTTP_Request = [System.Net.WebRequest]::Create('https://app.xxx.com/')
  Try
  {
  # We then get a response from the site.
  $HTTP_Response = $HTTP_Request.GetResponse()
  
  # We then get the HTTP code as an integer.
  $HTTP_Status = [int]$HTTP_Response.StatusCode
  
  If ($HTTP_Status -eq 200) {
      Write-Host "Site is OK!"
  }
  Else {
      Write-Host "The Site may be down, please check!"
      Restart-WebAppPool -Name "app.xxx.com"
  }
  }
  Catch
  {
       Stop-WebAppPool -Name "app.xxx.com"
       Restart-WebAppPool -Name "app.xxx.com"
  }
  
  # Finally, we clean up the http request by closing it.
  $HTTP_Response.Close()
  
   Start-Sleep -Seconds 120
  }
  </code></pre>
  
  <p>Error: </p>
  
  <blockquote>
    <p>Restart-WebAppPool : You have to start stopped object before
    restarting it. At C:\Scripts\CheckHealthHaydigo.ps1:25 char:6
    +      Restart-WebAppPool -Name "app.xxx.com"
    +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [Restart-WebAppPool], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperation,Microsoft.IIs.PowerShell.Provider.RestartAppPoolCommand</p>
  </blockquote>