[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script to bulk restart a service on many workstations

Posted on 2011-05-06
3
Medium Priority
?
798 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:patriots
3 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35710468
0
 
LVL 97

Accepted Solution

by:
Lee W, MVP earned 2000 total points
ID: 35710486
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
 
LVL 10

Expert Comment

by:ReneGe
ID: 35710724
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

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question