autoit 发表于 2008-5-3 14:19:59

AU3-添加服务

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

ollydbg 发表于 2008-5-10 22:06:52

wmi 复杂ying,高深看不懂了,俺继续加油吧。

sanhen 发表于 2008-5-10 22:15:23

用API的爽。。。发有UDF了的。:face (20):

天使也疯狂 发表于 2008-6-23 02:54:04

好厉害:face (37):

jtzxgfy 发表于 2008-7-7 01:17:50

看不懂,太深了

木纳 发表于 2008-7-7 09:05:15

看不懂就更要收藏先...呵呵

silentdream 发表于 2008-7-7 23:52:13

我要昏过去了
:face (3):

小费 发表于 2008-8-27 23:06:38

:face (11):   菜鸟学习ING,.,

amgloon 发表于 2008-8-28 15:05:58

复杂。菜鸟看不懂..啊俺.

f4李文杨 发表于 2008-8-29 17:25:42

这段好像在那里见过!!!呵呵其实都大同小意了!!

liyi-softs 发表于 2008-10-20 18:00:55

看不懂啊!!!!!

moscy 发表于 2008-12-6 11:13:16

看是看不太懂,不过很好~~很强大~

dntc 发表于 2008-12-7 15:04:08

太强大了。

goonx 发表于 2009-8-27 18:26:37

学习一下~~~

438767161 发表于 2009-8-31 21:59:31

俺还是个新手,来学习下。。。
页: [1] 2 3 4
查看完整版本: AU3-添加服务