Solved

Troubleshoot script for stopping/diabling services

Posted on 2008-06-19
5
205 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Preface Having the need * to contact many different companies with different infrastructures * do remote maintenance in their network required us to implement a more flexible routing solution. As RAS, PPTP, L2TP and VPN Client connections are no…
This is a little timesaver I have been using for setting up Microsoft Small Business Server (SBS) in the simplest possible way. It may not be appropriate for every customer. However, when you get a situation where the person who owns the server is i…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

707 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

17 Experts available now in Live!

Get 1:1 Help Now