找回密码
 加入
搜索
查看: 27690|回复: 47

AU3-添加服务

 火... [复制链接]
发表于 2008-5-3 14:19:59 | 显示全部楼层 |阅读模式
<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>

评分

参与人数 1金钱 +25 贡献 +10 收起 理由
502762378 + 25 + 10 虽然你许久么上线,但是你帮到了我,真诚的 ...

查看全部评分

发表于 2008-5-10 22:06:52 | 显示全部楼层
wmi 复杂ying,高深看不懂了,俺继续加油吧。
发表于 2008-5-10 22:15:23 | 显示全部楼层
用API的爽。。。发有UDF了的。
发表于 2008-6-23 02:54:04 | 显示全部楼层
好厉害
发表于 2008-7-7 01:17:50 | 显示全部楼层
看不懂,太深了
发表于 2008-7-7 09:05:15 | 显示全部楼层
看不懂就更要收藏先...呵呵
发表于 2008-7-7 23:52:13 | 显示全部楼层
我要昏过去了
发表于 2008-8-27 23:06:38 | 显示全部楼层
   菜鸟学习ING,.,
发表于 2008-8-28 15:05:58 | 显示全部楼层
复杂。菜鸟看不懂..啊俺.
发表于 2008-8-29 17:25:42 | 显示全部楼层
这段好像在那里见过!!!呵呵其实都大同小意了!!
发表于 2008-10-20 18:00:55 | 显示全部楼层
看不懂啊!!!!!
发表于 2008-12-6 11:13:16 | 显示全部楼层
看是看不太懂,不过很好~~很强大~
发表于 2008-12-7 15:04:08 | 显示全部楼层
太强大了。
发表于 2009-8-27 18:26:37 | 显示全部楼层
学习一下~~~
发表于 2009-8-31 21:59:31 | 显示全部楼层
俺还是个新手,来学习下。。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-12-22 22:15 , Processed in 0.083088 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表