Solved

vbscript to stop service on 64 bit machine

Posted on 2011-09-27
4
2,379 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
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…
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…

708 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

14 Experts available now in Live!

Get 1:1 Help Now