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

Frank McCourryV.P. Holland Computers, Inc.
CERTIFIED EXPERT
Published:
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 ( http://www.simpleservermonitor.com) or a similar service, 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
                      

Open in new window



Of course, using this method will cause all websites running on this server to restart.  Be careful with it's implementation.  This is not a cure for the problem, simply a workaround.  You should investigate the real problem and fix it.

You can also use this script to check for running services and restart them if they are not running.  Just modify the code by replacing the variables S1 through S5 with the names of the services you want to monitor.  If you have more or less services then you will need to change the lines that read FOR %%A IN (%s1% %s2% %s3% %s4% %s5%) to match the set statements in the beginning of the code.  (Hint:  There are two lines that need this adjustment)

You could run this script on a schedule, say once every hour, to ensure that critical services are running.  Things like SQL Server Agent or Quickbooks database engine would be great candidates for this.  

Since it is a batch file and it is looking at services, it will need to run with elevated permissions.  Make sure that you test, test and retest in a non production environment!

Enjoy!

(Note: This was originally published on my blog, and has been expanded for the Experts Exchange article.)
0
6,057 Views
Frank McCourryV.P. Holland Computers, Inc.
CERTIFIED EXPERT

Comments (1)

Frank McCourryV.P. Holland Computers, Inc.
CERTIFIED EXPERT

Author

Commented:
Thanks,  

It's my first time publishing articles here and I wasn't sure if I should but links to my blog in the article.  I will do so from now on.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.