Link to home
Start Free TrialLog in
Avatar of fmcsa004
fmcsa004

asked on

Troubleshoot script for stopping/diabling services

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

Avatar of tigermatt
tigermatt
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of fmcsa004
fmcsa004

ASKER

It's erroring out with a cannot ping "." when substituting for servername.  
ASKER CERTIFIED SOLUTION
Avatar of tigermatt
tigermatt
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Taht did the trick.  Thank you!
You're most welcome!
Have a good day,
--tigermatt