Solved

VBScript template for stopping/disabling services

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Ok I have been working on this for some time having learned and gained certification in XenDesktop 4 along came version 5 which was released last month. Since then I have been working to deploy XenDesktop 5 in a small environment with only 2 virt…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

13 Experts available now in Live!

Get 1:1 Help Now