Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vbs to stop Sophos services prior to removal

Posted on 2015-01-22
1
Medium Priority
?
294 Views
Last Modified: 2016-02-25
I have a script to stop all services as a pre-deployment task prior to uninstalling Sophos from a Windows 7 PC.
The works fine when I deploy manually on the PC and stops all the services. However via the deployment tool, the services don't stop. When I monitor processes I do see wscript running and among other things trustedinstaller. I am possibly thinking that the script runs, but the services haven't stopped before the uninstallation begins. The script is below, any idea on how to stop the script from finishing until all services were stopped? (to test this theory)

StopService ".", "Sophos Agent", True
StopService ".", "SAVService", True
StopService ".", "SAVAdminService", True
StopService ".", "Sophos AutoUpdate Service", True
StopService ".", "Sophos Device Control Service", True
StopService ".", "Sophos Message Router", True
StopService ".", "Sophos Web Control Service", True
StopService ".", "swi_service", True

Sub StopService(Computer, ServiceName, Wait)
  Dim cimv2, oService, Result

  'Get the WMI administration object    
  Set cimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    Computer & "\root\cimv2")

  'Get the service object
  Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
 
  'Stop the service
  Result  = oService.StopService
0
Comment
Question by:kwatt562
1 Comment
 
LVL 76

Accepted Solution

by:
David Lee earned 2000 total points
ID: 40567061
Try this

StopService ".", "Sophos Agent", True
StopService ".", "SAVService", True
StopService ".", "SAVAdminService", True
StopService ".", "Sophos AutoUpdate Service", True
StopService ".", "Sophos Device Control Service", True
StopService ".", "Sophos Message Router", True
StopService ".", "Sophos Web Control Service", True
StopService ".", "swi_service", True

Sub StopService(Computer, ServiceName, Wait)
	Const MAX_LOOPS = 20
	Dim cimv2, oService, Result, iLoop, bStopped
	
	'Get the WMI administration object    
	Set cimv2 = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
	Computer & "\root\cimv2")
	
	'Get the service object
	Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
	
	'Stop the service
	Result  = oService.StopService 
	
	'Test to see if the service has stopped
        bStopped = False
	For iLoop = 1 To MAX_LOOPS
		Set oService = cimv2.Get("Win32_Service.Name='" & ServiceName & "'")
		If oService.State = "Stopped" Then
                        bStopped = True
			Exit For
		Else
			WScript.Sleep 500 
		End If
	Next
        If Not bStopped Then
            Msgbox "I was unable to stop the " & ServiceName & " service.", vbCritical + vbOkOnly
        End If
 End Sub

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

916 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