Script to bulk restart a service on many workstations

I have a service on a few hundred Windows workstations I would like to restart using a script.  The goal is:
1. Run this script against a text file with just a simply list of computer names.
2. Restart a specific service on each of these systems.
3. Pipe to a log file

I've seen a few things doing a quick google search, but most of what I see shows the sc.exe command...that's fine, but I'd hate to have to to a separate "stop" and a separate "start" for each service...I don't see a "restart" switch with sc.exe, and I'm not sure how to use it to run it against a text list of computers.

this is probably easy enough, but I'm a noob to this type of scripting.  Prefer powershell, but could also do a vbscript...doesn't matter, just need help.
patriotsAsked:
Who is Participating?
 
Lee W, MVPConnect With a Mentor Technology and Business Process AdvisorCommented:
ALWAYS test such scripts on small samples first, but:

Assume your text file with computer names AND/OR IP addresses is as follows:
HRPC1
HRPC2
SALESPC1
SALESPC2
SALESPC3
CXOPC1
CXOPC2
etc

and those names are save to a file called PCS.TXT

now, in batch, assuming the service you want to stop is called "MyServiceName"

@Echo off
REM This sets the log file to C:\ServiceRestart.Log - change it if you like.
Set LogFile=C:\ServiceRestart.Log
REM Change the "MyServiceName" part to the name of your service that you want to restart.
Set ServiceToRestart=MyServiceName
FOR /f "tokens=*" %%a in (PCS.TXT) Do (
   Echo Stopping service %ServiceToRestart% on %a...>>%LogFile%
   SC \\%%a STOP %ServiceToRestart%
   If "%errorlevel%"=="1" (
      Echo **** ERROR STOPPING SERVICE ON %%a!>>%LogFile%
   ) Else (
      Echo Successfully stopped %ServiceToRestart% on %a>>%LogFile%
      SC \\%%a START %ServiceToRestart%
      If "%errorlevel%"=="1" Echo **** ERROR STARTING SERVICE ON %%a!>>%LogFile%
)

Open in new window


I think that should work - but I haven't tested it.  If it doesn't please take a screen shot of any error messages and post that screen shot so we can try to see why it didn't work as expected.
0
 
Aaron TomoskySD-WAN SimplifiedCommented:
0
 
ReneGeCommented:
Adding to leew's script, I found that all PCs in his example have "PC" in their name.

I then changed line 6 so you can see how to do this without even having a PC list text file.

Enjoy

@Echo off
REM This sets the log file to C:\ServiceRestart.Log - change it if you like.
Set LogFile=C:\ServiceRestart.Log
REM Change the "MyServiceName" part to the name of your service that you want to restart.
Set ServiceToRestart=MyServiceName
FOR /f %%a in ('NET VIEW ^| FINDSTR -i "pc"') Do (
   Echo Stopping service %ServiceToRestart% on %a...>>%LogFile%
   SC \\%%a STOP %ServiceToRestart%
   If "%errorlevel%"=="1" (
      Echo **** ERROR STOPPING SERVICE ON %%a!>>%LogFile%
   ) Else (
      Echo Successfully stopped %ServiceToRestart% on %a>>%LogFile%
      SC \\%%a START %ServiceToRestart%
      If "%errorlevel%"=="1" Echo **** ERROR STARTING SERVICE ON %%a!>>%LogFile%
)

Open in new window


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.