WMI Script to List Services

http://www.computerperformance.co.uk/vbscript/wmi_services.htm 这文章太好了。。

' Service.vbs
' Sample script to List services N-Z
' www.computerperformance.co.uk/
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.5 December 2010
' -------------------------------------------------------'
Option Explicit
Dim objWMIService, objItem, objService, strServiceList
Dim colListOfServices, strComputer, strService

'On Error Resume Next

' ---------------------------------------------------------
' Pure WMI commands
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service ")

' WMI and VBScript loop
For Each objService in colListOfServices
If UCase(Left(objService.name,1)) >"N" then
strServiceList = strServiceList & vbCr & _
objService.name

End if
Next

WScript.Echo strServiceList

' End of Example WMI script to list services


From a WMI perspective

1) If you are experienced with WMI, then the two features concentrate on are, Win32_Service and objService.name.

2)  If you are new to WMI then you will soon appreciate that all WMI scripts begin by employing winmgmts to access the root of the CIM library :
Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")

2) WMI often requires security clearance in order to query the other machine's hardware, this is why we add :  & "{impersonationLevel=impersonate}!\\" _  

3) Set colComputer = objWMIService.ExecQuery _ is a standard WMI phrase to prepare for the WQL command:  Select * from Win32_Service
".  The part we are particularly interested in is _Service.  Win32 has dozens of properties, here we need to query not the process, but the Service component.

From a VBScript perspective

4) What makes scripting so powerful is the speed with which VBScript loops through an array of objects or properties, in this instance the code to look out for is: For Each....In... Next. 

5)  I am particularly proud of the this command:strServiceList = strServiceList & vbCr & objService.name.  In scripting terms it's primitive, almost a non-entity, but to me it makes the output easier to read.

6) The only property of objService that we are interested in is, .Name. However, we could have substituted other properties, for example .State or .Status.

7) As I discussed at the outset, I deliberately decided to only display a subset of all possible services, I chose to filter with the command, If UCase(Left(objService.name,1)) >"N" then...  Incidentally, there is hardly a script that cannot be fine-tuned with an, If ... Then.. End If. statement.

8)  You could take a different road and output the service information to a file.  VBScript has all the tools you need to create a file and write a service on each line.  In that example, you would not need the If..Then filter.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值