[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Troubleshoot script for stopping/diabling services

Posted on 2008-06-19
5
Medium Priority
?
229 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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In a hurry?.. scroll down to "HERE's HOW TO DO IT" Section. Greetings All, I was going to post this as question/solution, but its seems more appropriate as an article considering its length.  I felt it important to illucidate all the details c…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

873 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