扶正楼主代码
- Func _CreateService($name, $displayname, $pathname, $startmode = "Automatic", $desktopinteract = True, $startname ="", $servicetype = 16, $errorcontrol = 0)
- ; Connect to WMI.
- $objServices = ObjGet("winmgmts:root\cimv2")
-
- ; Obtain the definition of the Win32_Service class.
- $objService = $objServices.Get ("Win32_Service")
-
- ; Obtain an InParameters object specific to the Win32_Service.Create method.
- $objInParam = $objService.Methods_ ("Create") .inParameters.SpawnInstance_ ()
-
- ;Add the input parameters.
- $objInParam.Properties_.item ("Name") = $name;< - Service Name
- $objInParam.Properties_.item ("DisplayName") = $displayname;< - Display Name, what you see in the Services control panel
- $objInParam.Properties_.item ("PathName") = $pathname;< - Path and Command Line of the executable
- $objInParam.Properties_.item ("ServiceType") = $servicetype
- $objInParam.Properties_.item ("ErrorControl") = $errorcontrol
- $objInParam.Properties_.item ("StartMode") = $startmode
- $objInParam.Properties_.item ("DesktopInteract") = $desktopinteract
- If not $startname = "" Then
- $objInParam.Properties_.item("StartName") = $startname;< - If null, will run as Local System
- EndIf
- If not $password = "" And not $startname ="" Then
- $objInParam.Properties_.item("StartPassword") = $password;< - Only populate if the SatrtName param is populated
- EndIf
- ;More parameters and return statuses are listed in MSDN: "Create Method of the Win32_Service Class"
- ; Execute the method and obtain the return status.
- ; The OutParameters object in objOutParams is created by the provider.
- $objOutParams = $objService.ExecMethod_ ("Create", $objInParam)
- ConsoleWrite($objOutParams)
- EndFunc
- Func _DeleteService($name)
- Dim $objWMIService, $objItem, $objService
- Dim $colListOfServices, $strService
- ; NB strService is case sensitive.
- $strService = $name;< - Service Name
- ; Connect to WMI.
- $objWMIService = ObjGet("winmgmts:root\cimv2")
- $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")
- For $objService in $colListOfServices
- $objService.Delete()
- Next
- EndFunc
- Func _StartService($name)
- Dim $objWMIService, $objItem, $objService
- Dim $colListOfServices, $strService
- ; NB strService is case sensitive.
- $strService = $name;< - Service Name
- ; Connect to WMI.
- $objWMIService = ObjGet("winmgmts:root\cimv2")
- $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")
- For $objService in $colListOfServices
- $objService.StartService()
- Next
- EndFunc
- Func _StopService($name)
- Dim $objWMIService, $objItem, $objService
- Dim $colListOfServices, $strService
- ; NB strService is case sensitive.
- $strService = $name;< - Service Name
- ; Connect to WMI.
- $objWMIService = ObjGet("winmgmts:root\cimv2")
- $colListOfServices = $objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '" & $strService & "'")
- For $objService in $colListOfServices
- $objService.StopService()
- Next
- EndFunc
复制代码 |