Solved

vbs to stop Sophos services prior to removal

Posted on 2015-01-22
1
231 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

15 Experts available now in Live!

Get 1:1 Help Now