Solved

VBScript template for stopping/disabling services

Posted on 2008-06-18
4
5,429 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
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now