Recipe7.3.Removing a Service


Recipe 7.3. Removing a Service

Problem

You want to remove a service. When you uninstall an application that previously installed a service, the service will automatically be removed. However, if you manually installed the service as described in Recipe 7.2, you'll need to manually remove it to uninstall it.

Solution

Before you can remove a service, you need to make sure the service is not running. See Recipe 7.1 for more on stopping a service.

Using a graphical user interface

  1. Open the Service Creation Wizard (srvinstw.exe).

  2. Select Remove a service and click Next.

  3. Select the target machine from which to remove the service and click Next.

  4. Select the service you want to remove and click Next.

  5. Click Finish.

  6. Click OK to confirm removal of the service.

Using a command-line interface:

The following commands stop a service and remove it:

> sc <ServiceName> stop > instsrv <ServiceName> remove

Using VBScript
' This code removes a service. ' ------ SCRIPT CONFIGURATION ------ strComputer     = "<ServerName>"  ' e.g., fs-rtp01 (use . for local server) strSvcName      = "<ServiceName>" ' e.g., MyMonitor boolStopService = TRUE            ' e.g., TRUE to attempt to stop the service ' ------ END CONFIGURATION ---------     set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") set objService = objWMI.Get("Win32_Service.Name='" & strSvcName & "'") if boolStopService = TRUE then    intRC = objService.StopService        WScript.Sleep 5000  ' Give the service 5 seconds to stop        if intRC > 0 then       WScript.Echo "Error stopping service: " & intRC       WScript.Quit    else       WScript.Echo "Successfully stopped service"    end if end if     intRC = objService.Delete if intRC > 0 then    WScript.Echo "Error deleting service: " & intRC else    WScript.Echo "Successfully deleted service" end if

Discussion

Anytime you uninstall an application, the uninstall program should automatically remove the services that were installed with the application. If you find that it doesn't, you should complain loudly to the company that made that software. If, however, you manually created your own service, the only way to remove it when you no longer want it is to manually delete it. All three solutions use a slightly different method for removing a service, but they are all essentially performing the same task. Removing a service consists of removing the associated entries from the registry (HKLM\SYSTEM\CurrentControlSet\Services\<ServiceName>) and deregistering the service with the SCM.

See Also

Recipe 7.2 and MSDN: Delete Method of the Win32_Service Class



Windows Server Cookbook
Windows Server Cookbook for Windows Server 2003 and Windows 2000
ISBN: 0596006330
EAN: 2147483647
Year: 2006
Pages: 380
Authors: Robbie Allen

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net