Batch script to restart IIS services. Can be modified for other purposes.

If you have ever had a server that is has difficulty with the iisreset command, this may help.  In my testing it works on everything from Windows 2000 to 2008R2.  Your mileage may vary. We use this to automate resets on a stubborn server.

Using Simple Server Monitor, we check a website for a response. If no response is given, then this script will fire. Simple Server Monitor has the ability to pass parameters to the command line and makes this an easy solution.

This batch script will first check to see if the specified services are running, attempt to stop them, restart them, check to see if they are running and if they do not start after a few tries, reboots the system.

@ECHO OFF

::Get Server name from command line. Be sure to specify it as \\servername
set server=%1

::Set Variables for Services you can define any service you want here and as many as you want. Just add or remove variables. Make sure the FOR %%A statements all have the same variables.
set s1=W3SVC
set s2=HTTPFilter
set s3=SMTPSVC
set s4=MSFtpsvc
set s5=iisadmin

::Setup the counter. I use 1 here because I like to start counting at 1, not 0. It's a kindergarten thing...
set /A count=1

:Service_Stop
::Stop All Services. If it is already stopped, move on.
ECHO.
ECHO. Stop Services
ECHO.

::Make sure the line below has the same variables defined in the beginning.
FOR %%A IN (%s1% %s2% %s3% %s4% %s5%) DO (
sc %server% query %%A
for /f "tokens=3" %%a in ('sc %server% query %%A^|find "STATE"') do (
if %%a EQU 4 ECHO. %%A SERVICE IS RUNNING
if %%a EQU 4 sc %server% stop %%A
if %%a EQU 4 ping -n 20 127.0.0.1 >nul
if %%a NEQ 4 ECHO. %%A SERVICE IS NOT RUNNING
)
)

:Service_Start
::Start All Services. If it is already running, move on.
ECHO.
ECHO. Start Services
ECHO.
ECHO. This is attempt number %count%
ECHO.

IF %count% EQU 4 GOTO REBOOT
::Make sure the line below has the same variables defined in the beginning.
FOR %%A IN (%s1% %s2% %s3% %s4% %s5%) DO (
for /f "tokens=3" %%a in ('sc %server% query "%%A"^|find "STATE"') do (
if %%a NEQ 4 sc %server% start %%A
if %%a NEQ 4 ECHO Attempting to start %%A on %server%
if %%a NEQ 4 ping -n 20 127.0.0.1 >nul
)
)

:Check_Status
::Check to see if all services are running, if not go back and try again. If so, finish up.
ECHO.
ECHO. Check Service Status
ECHO.

::Make sure the line below has the same variables defined in the beginning.
FOR %%A IN (%s1% %s2% %s3% %s4% %s5%) DO (
sc %server% query %%A
for /f "tokens=3" %%a in ('sc %server% query %%A^|find "STATE"') do (
if %%a EQU 4 ECHO. %%A SERVICE IS RUNNING
if %%a NEQ 4 ECHO. %%A SERVICE IS NOT RUNNING
if %%a NEQ 4 SET /A count=%count%+1
if %%a NEQ 4 ping -n 20 127.0.0.1 >nul
if %%a NEQ 4 GOTO Service_Start
)
)
:END
::Cleanup First. Make sure that you define the same variables as defined in the begining.
set server=
set s1=
set s2=
set s3=
set s4=
set s5=
set count=
:: Make sure we bypass the REBOOT Section - Whew! That was close!
GOTO:EOF

:REBOOT
:: Last ditch effort to get the services started.
ECHO.
ECHO. REBOOTING BECAUSE I CANNOT START ALL OF THE SERVICES
ECHO.
REM shutdown /r /m %server% /t 30 /d u:0:0 /c "Unable to start services from IIS Reset Script - FJM"
:: Call it a done deal, clean up, and let's get out of here!
GOTO END

 

Thanks to Rob van der Woude’s Scripting Pages for the help getting this one off the ground.