Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vbscript to stop service on 64 bit machine

Posted on 2011-09-27
4
Medium Priority
?
2,574 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
jawa29 earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

715 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