Solved

vbscript to stop service on 64 bit machine

Posted on 2011-09-27
4
2,458 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:ltlbearand3
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
jawa29 earned 500 total points
ID: 36710764
Hi

You might be having issues because this code uses the Win32_Service, now because I don't believe there is a Win64_Service to use you could try the following.

Set oShell = WScript.CreateObject("WScript.Shell")
oShell.Run "net stop " & strService,,True

Jawa29
0
 
LVL 20

Assisted Solution

by:ltlbearand3
ltlbearand3 earned 0 total points
ID: 36712595
Thanks.  I had stayed away from net stop and used WMI so I could check the status and did not even think about combining the two approaches.  Net Stop does work on 64, but required UAC elevation where appropriate.  I changed the wait flag to FALSE, otherwise the script can hang if there are dependencies.  My original script would just fail to stop the service.  Here is the start of what got me working:

strService = "MyLogService""
If WScript.Arguments.length =0 Then
  Set objShell = CreateObject("Shell.Application")
  'Pass a bogus argument with leading blank space, say [ uac]
  objShell.ShellExecute "wscript.exe", Chr(34) & _
  WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else

Set objShell = CreateObject("WScript.Shell")
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
		objShell.Run "net stop " & objService.Name,0,False
		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
End if

Open in new window

0
 
LVL 20

Author Closing Comment

by:ltlbearand3
ID: 36898971
Thanks.
0
 
LVL 20

Author Comment

by:ltlbearand3
ID: 36900994
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.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

821 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