Link to home
Start Free TrialLog in
Avatar of mystikal1000
mystikal1000

asked on

Batch script to restart Print Spooler service on 100+ servers

I need a script that will be able to restart the print server service on over 100+ 2003 servers.  The best way is for the script to read the servers.txt file and restart the service on all of the server in the txt. file.  I need this asap.  I appreciate it.
Avatar of Kent Dyer
Kent Dyer
Flag of United States of America image

You mean something like:

FOR /F "eol=; tokens=* " %%F IN ('TYPE SERVERS.txt') DO (
PSEXEC \\%%F NET STOP Tomcat6
SLEEP 2
PSEXEC \\%%F NET STOP Tomcat6
)

Open in new window


PSEXEC is from SysInternals...

HTH,

Kent
Doh!

Let's try that again..

FOR /F "eol=; tokens=* " %%F IN ('TYPE SERVERS.txt') DO (
PSEXEC \\%%F NET STOP Spooler
SLEEP 2
PSEXEC \\%%F NET START Spooler
)

Open in new window


HTH,

Kent
Note: This is Batch Script and you will need to save it as a .BAT or .CMD..

The %%F is important within a BAT file..  If you were to run through a command line, it would be: %F..

HTH,

Kent
Avatar of oBdA
oBdA

This script should restart the spooler service on all servers (more or less) parallel (so don't be alarmed if a lot of command boxes pop up); it requires PsService.exe from Sysinternals. Try it with list with only a few servers first:
@echo off
setlocal
set ServerFile=servers.txt
for /f %%a in ('type "%ServerFile%"') do (
  start "" psservice.exe restart "Spooler"
)

Open in new window

PsService v2.24
http://technet.microsoft.com/en-us/sysinternals/bb897542
Avatar of mystikal1000

ASKER

I can't use pexec.exe its blocked within firewall.  Any other script or vbs that can do?
You mean like this..

RestartService(Spooler, COMPUTERNAME, TRUE)

' -- Sub is taken from: http://www.robvanderwoude.com/vbstech_proc_service.php
Sub RestartService(myService, StrComputer, blnQuiet)
	' This subroutine restarts a service
	' Arguments:
	' myService     use the service's DisplayName
	' blnQuiet      if False, the state of the service is displayed
	'               every second during the restart procedure
	'
	' Written by Rob van der Woude
	' http://www.robvanderwoude.com
		
	' Standard housekeeping
	Dim colServices, colServicesTest, objService
	Dim objServiceTest, objWMIService, strQuery, strTest
	
	' Create a WMI object
	Set objWMIService = GetObject("winmgmts:\\" & StrComputer & "\root\CIMV2")
		
	' Query the services for "our" service
	strQuery = "SELECT * FROM Win32_Service WHERE DisplayName='" & myService & "'"
	Set colServices = objWMIService.ExecQuery(strQuery, "WQL", 48)
		
	' Loop through the "collection" of returned services
	For Each objService In colServices
		' See if we need to tell the user we're going to stop the service
		If Not blnQuiet Then
			WScript.Echo "Stopping " & myService
		End If
		
		' Stop the service
		objService.StopService
				
		' Wait until the service is stopped
		Do Until strTest = "Stopped"
			' Create a new object for our service; this work-around is required
			' since otherwise the service's state information isn't properly updated
			Set colServicesTest = objWMIService.ExecQuery(strQuery, "WQL", 48)
						
			' Loop through the "collection" of returned services
			For Each objServiceTest In colServicesTest
				' Check the service's state
				strTest = objServiceTest.State
				' See if we need to show the progress
				If Not blnQuiet Then
					WScript.Echo "State: " & strTest
				End If
				' Wait 1 second
				' -- Changed to 5 seconds (5000): Kent Dyer
				WScript.Sleep 5000
			Next
						
			' Clear the temporary object
			Set colServicesTest = Nothing
		Loop
				
		' See if we need to tell the user we're going to (re)start the service
		If Not blnQuiet Then
			WScript.Echo "Starting " & myService
		End If
		
		' Start the service
		objService.StartService
				
		' Wait until the service is running again
		Do Until strTest = "Running"
			' Create a new object for our service; this work-around is required
			' since otherwise the service's state information isn't properly updated
			Set colServicesTest = objWMIService.ExecQuery(strQuery, "WQL", 48)
						
			' Loop through the "collection" of returned services
			For Each objServiceTest In colServicesTest
				' Check the service's state
				strTest = objServiceTest.State
				' See if we need to show the progress
				If Not blnQuiet Then
					WScript.Echo "State: " & strTest
				End If
				' Wait 1 second
				WScript.Sleep 1000
			Next
						
			' Clear the temporary object
			Set colServicesTest = Nothing
		Loop
	Next
End Sub

Open in new window


HTH,

Kent
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You may have to make a tweak with the one that I provided..

Instead of..
	strQuery = "SELECT * FROM Win32_Service WHERE DisplayName='" & myService & "'"
	Set colServices = objWMIService.ExecQuery(strQuery, "WQL", 48)

Open in new window


use..
	strQuery = "SELECT * FROM Win32_Service WHERE Name='" & myService & "'"
	Set colServices = objWMIService.ExecQuery(strQuery, "WQL", 48)

Open in new window



If you choose to use DisplayName, it is "Print Spooler"

Name is: "Spooler"

HTH,

Kent
One option if you got firewall problems

Just Scheduled a task on each server to run batch file every 24 hours at some early time in morning. With error check to send an email if there a problem


NET STOP Spooler
SLEEP 2
NET START Spooler

Just use

NET Start command to check to see if Spooler is running or not