创建删除服务

Function DeleteService(SrvName)
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colListOfServices = objWMIService.ExecQuery _
        ("Select * from Win32_Service Where Name = '" & SrvName & "'")

    For Each objService in colListOfServices
        objService.StopService()
        objService.Delete()
    Next
    If Err.Number <> 0 Then
        DeleteService = 0
    Else
        DeleteService = 1
    End If
End Function 

Sub InstallService(SrvName,DispName,Path)
    Const OWN_PROCESS = 16
    Const NOT_INTERACTIVE = False
    Const NORMAL_ERROR_CONTROL = 2

    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set objService = objWMIService.Get("Win32_Service")

    errReturn = objService.Create(SrvName,DispName, _
        Path, OWN_PROCESS, NORMAL_ERROR_CONTROL,_
            "Automatic", NOT_INTERACTIVE, "LocalSystem", ""  )
End Sub

Service Properties Exposed to the WMI Create Method

Position Property
1 Name – Name of the service as stored in the registry.
2 DisplayName – Name of the service as displayed in the Services snap-in.
3 PathName – Full path to the service’s executable file.
4 ServiceType – Type of service.
5 ErrorControl – Action to be taken should a service fail during startup.
6 StartMode – Method used to start the service.
7 DesktopInteract – Indicates whether the service can create or communicate with windows on the desktop.
8 StartName – Account name under which the service runs.
9 StartPassword – Password for the account name specified by StartName.
10 LoadOrderGroup – Load-ordered group to which the service belongs.
11 LoadOrderGroupDependencies – Set of load-ordered groups that must be running before this service can start.
12 ServiceDependencies – Services that must be running before this service can start.

Service Method Error Codes
Value Description
0 The request was accepted.
1 The request is not supported.
2 The user did not have the necessary access.
3 The service cannot be stopped because other services that are running are dependent on it.
4 The requested control code is not valid, or it is unacceptable to the service.
5 The requested control code cannot be sent to the service because the state of the service (Win32_BaseService State property) is equal to 0, 1, or 2.
6 The service has not been started.
7 The service did not respond to the start request in a timely fashion.
8 Unknown failure when starting the service.
9 The directory path to the service executable file was not found.
10 The service is already running.
11 The database to add a new service is locked.
12 A dependency this service relies on has been removed from the system.
13 The service failed to find the service needed from a dependent service.
14 The service has been disabled from the system.
15 The service does not have the correct authentication to run on the system.
16 This service is being removed from the system.
17 The service has no execution thread.
18 The service has circular dependencies when it starts.
19 A service is running under the same name.
20 The service name has invalid characters.
21 Invalid parameters have been passed to the service.
22 The account under which this service runs is either invalid or lacks the permissions to run the service.
23 The service exists in the database of services available from the system.
24 The service is currently paused in the system.

发表回复