Solved

Troubleshoot script for stopping/diabling services

Posted on 2008-06-19
5
213 Views
Last Modified: 2010-04-21
Could someone please let me know how to modify this script for use on the local machine only.  I just need to disable the services on the local machine, not a remotely located.  I'm not sure what the syntax is to specify local machine.  Thank you.
' Note: Each Service is case sensitive.
arrServices = Array("Telephony","Windows Audio")
 
' Server to stop services on
strServer = "servername"
 
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
Comment
Question by:fmcsa004
  • 3
  • 2
5 Comments
 
LVL 58

Expert Comment

by:tigermatt
ID: 21825621
It's using WMI, so you should be able to just change the server name to a . to represent the local system.

-tigermatt
' Note: Each Service is case sensitive.
arrServices = Array("Telephony","Windows Audio")
 
' Server to stop services on
strServer = "."
 
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
 

Author Comment

by:fmcsa004
ID: 21831806
It's erroring out with a cannot ping "." when substituting for servername.  
0
 
LVL 58

Accepted Solution

by:
tigermatt earned 125 total points
ID: 21832688
Oops, in that case, just use 127.0.0.1 as the IP in the ping as per the attached.
' Note: Each Service is case sensitive.
arrServices = Array("Telephony","Windows Audio")
 
' Server to stop services on
strServer = "."
 
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)
If strComputer = "." Then
pingPC="127.0.0.1"
else
pingPC=strComputer
End If
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & pingPC, 0, True)
	If boolCode = 0 Then
		Ping = True
	Else
		Ping = False
	End If
End Function

Open in new window

0
 

Author Closing Comment

by:fmcsa004
ID: 31468944
Taht did the trick.  Thank you!
0
 
LVL 58

Expert Comment

by:tigermatt
ID: 21834321
You're most welcome!
Have a good day,
--tigermatt
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

856 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