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.
Microsoft DOSVB ScriptIT Administration

Avatar of undefined
Last Comment
babesia

8/22/2022 - Mon
Kent Dyer

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
Kent Dyer

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
Kent Dyer

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
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
mystikal1000

ASKER
I can't use pexec.exe its blocked within firewall.  Any other script or vbs that can do?
Kent Dyer

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Kent Dyer

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
babesia

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