Solved

vbscript to stop service on 64 bit machine

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Have you ever had a hard drive that you can't boot into, but need to change the registry? Here is the solution! This article guides you through accessing and editing a registry of a non-primary drive. To read registry information on a non-prim…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
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…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

770 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