Question : General Purpose Server Reboot Script

Hi,

We have a variety of Windows servers in our environment & need to reboot them on a regular basis.

What I would like to do is write a general purpose script that would do the following:

Check if a given service needs to be stopped.  If it exists stop it.

For example, shutdown exchange first if it is running on the machine.
Then shutdown any SQL services if they exist
And so on

Would the /start wait command in conjunction with net stop achieve this best or should VBScript be involved.  I'd like to make the script usable on all our server regardless of what they are running.  

Answer : General Purpose Server Reboot Script

The following script will check each of the services listed on line 2.  
For services that match, the service will be asked to stop.
The script will wait to hear back before moving onto the next service.
By using the DCOM service as a test of a service that cannot be stopped, the script waits a few seconds then moves on to the next service.
In the looping process is where you want your business logic regarding stopping the application.  A return value of zero means the service was stopped.  A five is either a failure to stop or the service is already stopped.
I would recomend against forcing the script to find the underlying software and stopping it.  By doing so will be the same as pulling the plug on the service - who knows what might happen.
As an aside, scripting does not support multi-threads so managing many services all at once might bloat the script somewhat.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
strComputer = "."
strService = "Name = 'Alerter' OR Name = 'DCOMLaunch' OR Name = 'DevManBE' "
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service WHERE " & strService)
For Each objService in colListOfServices
	return = objService.StopService()
	'A return of 0 = success
	'A return of 5 = failed to stop or is already stopped
Next
wscript.echo "Done."
Open in New Window Select All
Random Solutions  
 
programming4us programming4us