Solved

vbs to stop Sophos services prior to removal

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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