Solved

VBScript template for stopping/disabling services

Posted on 2008-06-18
4
5,434 Views
Last Modified: 2008-07-28
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
Comment
Question by:fmcsa004
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Accepted Solution

by:
sr75 earned 63 total points
ID: 21816276
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
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 62 total points
ID: 21817429
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

739 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