Link to home
Start Free TrialLog in
Avatar of ltlbearand3
ltlbearand3Flag for United States of America

asked on

vbscript to stop service on 64 bit machine

I have a script that runs great on 32 bit systems.  However, when I run it on a 64 bit system, it fails to stop the service.  It finds the service and adds it to the collection.  However, the line:
objService.Stopservice()

Open in new window


Does not seem to work on 64 bit systems.  Anyone see what I am missing?

I have tried elevating with UAC and running Wscript from c:\Windows\System32.  Here is my script.
strService = "MyLogService""

Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\.\root\cimv2")


Set colListOfServices = objWMIService.ExecQuery _
	("Select * from Win32_Service Where Name = '" & strService & "'")

' Check if the service has been found
If colListOfServices.Count > 0 Then 
 
	' Stop Service
	For Each objService in colListOfServices
		objService.Stopservice()
		strStatus = objService.state
	Next

	' Verify Services have stopped
	counter = 0
	Do While Not strStatus = "Stopped"
		Set colListOfServices = objWMIService.ExecQuery _
			("Select * from Win32_Service Where Name = '" & strService & "'")
		For Each objService in colListOfServices
			strStatus = objService.state
			wscript.sleep 1000
			counter = counter + 1
			If strStatus = "Stopped"  Then
				msgbox objService.Name & " has been stopped"
			End IF
			If counter > 5 then
				msgbox "Failed to Stop " & objService.Name
				strStatus = "Stopped"
			End IF
		Next
	Loop ' Do While Not Stopped
End If ' Count > 0

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jawa29
jawa29
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
SOLUTION
Avatar of ltlbearand3
ltlbearand3
Flag of United States of America 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
Avatar of ltlbearand3

ASKER

Thanks.
One additional side note if someone else is looking at this.  I ended up changing this line:
objShell.Run "net stop " & objService.Name,0,False

Open in new window

to
objShell.Run "net stop """ & objService.Name & """",0,False

Open in new window


If the service name had spaces in it, this line failed.  With the qoutes it will now run on those also.