1、引用microsoft wmi scripting
2、Dim objApp As WbemScripting.SWbemLocator
Dim objServer As WbemScripting.SWbemServices
Dim objSet As WbemScripting.SWbemObjectSet
Dim obj As WbemScripting.SWbemObject
3、产生objServer的方法很多。以下三种都试过,都可以用。但现在还不知区别在哪里
这里都是用本机,所以没有给机器名。用的是“.”
a. Set objApp= CreateObject("WbemScripting.SWbemLocator") Set objServer = objApp.ConnectServer()
b. 不用objApp Set objServer = CreateObject("winmgmts://.")
c. 类似于 b,不用CreateObject,而用 GetObject Set objServer = GetObject("winmgmts://.")
a. Set objSet= objServer.InstancesOf("Win32_Service")
b. Set objSet= objServer.ExecQuery("select * from Win32_Service ")
Set objSet = objServer.ExecQuery("select * fromWin32_Service WHERE NAME = 'MSsqlSERVER' ")
5、取状态之类的。
For Each obj In objSet
Debug.Print obj.DisplayName,obj.Status
Next
MSDN有更详细说明
http://msdn.microsoft.com/zh-cn/library/ms974579.aspx
原文链接:https://www.f2er.com/vb/259503.html