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