title: How to loop in powershell until successful?
body: <pre><code>%WINDIR%\system32\inetsrv\appcmd.exe set site /site.name:"WebRole_IN_0_CB" /[Path='/'].applicationPool:"ASP.NET v4.0" &gt;&gt;CBLog.log
  powershell.exe -command Set-ExecutionPolicy Unrestricted
  powershell.exe .\CheckIfSuccessful.ps1
  </code></pre>
  
  <p>I would like to run the script above and if appcmd.exe can't execute it due the WebSite not yet being up and ready I would get the following error message in the CBLog.log file.</p>
  
  <blockquote>
    <p>ERROR ( message:Cannot find SITE object with identifier
    "WebRole_IN_0_CB". )</p>
  </blockquote>
  
  <p>In the CheckIfSuccessful.ps1 I would like to create a Powershell script to loop, which runs the appcmd command and checks the error again. Then sleeps for 5 seconds and then retries, until it succeeds.</p>
  
  <p>How would I do this in Powershell?</p>
  
  <p>highly appreciated,</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Ok, many thanks for the tip with $lastexitcode, while it looks promising. It seems I have problems with converting single quoates in  Powershell: (The below is part of my initial appcmd command)</p>
  
  <pre><code>/[Path='/'].applicationPool:"ASP.NET v4.0"
  </code></pre>
  
  <p>How do I quote this in powershell? I tried to simplify it so that powershell has less trouble with it, but it seems my command is now wrong, as it says now:</p>
  
  <blockquote>
    <p><strong>ERROR ( message:Cannot find SITE object with identifier "v4.0". )</strong></p>
  </blockquote>
  
  <pre><code> &amp; $Env:WinDir\system32\inetsrv\appcmd.exe set site '/site.name:"WebRole_IN_0_CB"' "/[Path='/'].applicationPool:`"ASP.NET v4.0`"" &gt;CBLog.log
  if($lastexitcode -ne '0')
  {
      while($lastexitcode -ne '0')
      {
          Start-Sleep -s 5
          &amp; $Env:WinDir\system32\inetsrv\appcmd.exe set site '/site.name:"WebRole_IN_0_CB"' "/[Path='/'].applicationPool:`"ASP.NET v4.0`"" &gt;CBLog.log
      }
  }
  </code></pre>
  
  <p><strong>UPDATE II:</strong>
  I have escaped it properly. But still I get the error message. If I run the appcmd alone after creating that site, the log file is ok with it. Any suggestions please?</p>