Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5437
  • Last Modified:

VBScript template for stopping/disabling services

I need to stop and disable several services in MS Windows XP Pro via a vbscript.  Can someone assist me with a script template for this?
0
fmcsa004
Asked:
fmcsa004
2 Solutions
 
sr75Commented:
Here you go!!!
On Error Resume Next
 
dim strComputer
 
strComputer="."
 
'Stop  and Start a Service
strServiceName = "Alerter"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"  & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery("Select * from Win32_Service Where Name ='" & strServiceName & "'")
For Each objService in colListOfServices
	objService.StopService()
	wscript.sleep 1500
	objService.StartService()
	wscript.echo objService.name	
next

Open in new window

0
 
RobSampsonCommented:
Hi, here's another version, and this will help too:
Hey, Scripting Guy! How Can I Disable a Service?
http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb06/hey0227.mspx

Regards,

Rob.
' Note: Each Service is case sensitive.
arrServices = Array("Print Spooler","Windows Audio","VNC Server")
 
' Server to stop services on
strServer = "D09790RING"
 
If Ping(strServer) = True Then
	For Each strService In arrServices
		Set objWMIService = GetObject("winmgmts:" _
			& "{impersonationLevel=impersonate}!\\" _
			& strServer & "\root\cimv2")
		Set colListOfServices = objWMIService.ExecQuery _
			("Select * from Win32_Service Where DisplayName='"& strService & "'")
		On Error Resume Next
		For Each objService In colListOfServices
			If Err.Number = 0 Then
				On Error GoTo 0
				If objService.State = "Running" Then
					objService.StopService()
					Wscript.Sleep 5000
				End If
				objService.ChangeStartMode "Disabled"
			Else
				Err.Clear
				On Error GoTo 0
				WScript.Echo strService & " service was not found on " & strServer
			End If
		Next
	Next
Else
	WScript.Echo strServer & " could not be pinged."
End If
 
WScript.Echo "Script finished."
 
Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now