Func _CreateService($name, $displayname, $pathname, $startmode = "Automatic", $desktopinteract = True, $startname = "", $servicetype = 16, $errorcontrol = 0)
Local $password
$objServices = ObjGet("winmgmts:root\cimv2")
$objService = $objServices.Get("Win32_Service")
$objInParam = $objService.Methods_("Create") .inParameters.SpawnInstance_()
$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
$objOutParams = $objService.ExecMethod_("Create", $objInParam)
ConsoleWrite($objOutParams)
EndFunc ;==>_CreateService
;删除服务
Func _DeleteService($name)
Dim $objWMIService, $objItem, $objService
Dim $colListOfServices, $strService
$strService = $name;< - Service Name
$objWMIService = ObjGet("winmgmts:root\cimv2")
$colListOfServices = $objWMIService.ExecQuery("Select * from Win32_Service Where Name = '" & $strService & "'")
For $objService In $colListOfServices
$objService.Delete()
Next
EndFunc ;==>_DeleteService
;开始服务
Func _StartService($name)
Dim $objWMIService, $objItem, $objService
Dim $colListOfServices, $strService
$strService = $name;< - Service Name
$objWMIService = ObjGet("winmgmts:root\cimv2")
$colListOfServices = $objWMIService.ExecQuery("Select * from Win32_Service Where Name = '" & $strService & "'")
For $objService In $colListOfServices
$objService.StartService()
Next
EndFunc ;==>_StartService
;停止服务
Func _StopService($name)
Dim $objWMIService, $objItem, $objService
Dim $colListOfServices, $strService
$strService = $name;< - Service Name
$objWMIService = ObjGet("winmgmts:root\cimv2")
$colListOfServices = $objWMIService.ExecQuery("Select * from Win32_Service Where Name = '" & $strService & "'")
For $objService In $colListOfServices
$objService.StopService()
Next
EndFunc ;==>_StopService
创建服务,并不一定使用这些函数就能成功加载,有些还得在注册表中写入键值,才能成功加载或启动服务。 |