'Halt batch file until website stop is complete?

This question is very similar to Halt batch file until service stop is complete? but I would like to stop a website instead. I currently have appcmd stop sites "siteName" in the script, but the batch file just issues the stop command and continues. I would like for the batch file to wait until the site is stopped before continuing. I'm running Windows Server 2008.



Solution 1:[1]

I could not figure out how to get this to work using a batch file. Something like this might work (but this probably won't work for you and doesn't work for some people):

:LoopWhileNotStopped

for /f "delims=" %x in ('appcmd list site "siteName"') do set siteInfo="%x"

IF NOT %siteInfo:~-16% == "state:Stopped)"
    GOTO :LoopWhileNotStopped

But I couldn't figure out how to modify the set siteInfo="%x" command so that it would work for the output of the appcmd command. The problem seemed to be that the appcmd output included double quotes and that interfered with command interpretation when the variable expansion is performed.

Here's a PowerShell script that should work:

C:\Windows\System32\inetsrv\appcmd.exe stop site "siteName"

do { Start-Sleep -s 1; $siteInfo = C:\Windows\System32\inetsrv\appcmd.exe list site "siteName" } until ($siteInfo -match "state:Stopped")

Solution 2:[2]

Does appcmd return immediately no matter what? This might work, depending on how appcmd behaves.

start /wait "site stop" appcmd stop sites "siteName"

If appcmd is a script, you could probably fix it so that it doesn't return immediately.

What web server are you using?

Solution 3:[3]

Obviously you are using IIS 7 or newer if you are using APPCMD.

  1. I think you mean SITE rather than SITES.
  2. You might have another file name APPCMD in your path. Type APPCMD /? to see if you are executing what you think.
  3. Try this to be sure you are executing the desired APPCMD:

    "%windir%\system32\inetsrv\appcmd.exe" stop site "siteName"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 mojo
Solution 3