windows wmi
介绍Windows Management Instrumentation(WMI)是Windows的一组扩展,可提供有关计算机系统的信息和通知。 WMI允许脚本语言(如VBScript或Windows PowerShell)在本地和远程管理Microsoft Windows计算机。 WMI已预安装在Windows 2000和更高版本的操作系统中。 可以从Windows NT,Windows 95和Windows 98下载该文件。
WMI允许您执行各种操作,包括但不限于:
- 获取正在运行的进程的列表
- 获取已安装程序的列表
- 缺少启动程序
- 获取磁盘使用率
- 获取计算机的品牌,型号和序列号
- 获取当前的内存和CPU使用率
- 创建新流程时获取通知
- 在计算机进入或退出睡眠模式时获取通知
- 创建并终止进程
- 关闭电脑
您可以从位于以下位置的Microsoft文档中找到有关WMI的更多信息:
WMI参考(Windows) 。 感谢NeoPa提供的链接。 示例VBA实施以下是VBA中的示例实现,该实现监视何时要将计算机置于睡眠模式并在该事件发生时退出Access。
您将需要添加对Microsoft WMI脚本V#。#库的引用。 第一个代码块在类模块中。 第二个代码块采用某种形式。
类模块
Option Explicit
'This is to allow the variable sink to raise events
Dim WithEvents sink As SWbemSink
Private Sub sink_OnObjectReady(ByVal objWbemObject As SWbemObject, ByVal objWbemAsyncContext As SWbemNamedValueSet)
'Event Type 4 is the code for entering sleep mode
If objWbemObject.EventType = 4 Then
Application.Quit
End If
End Sub
Private Sub Class_Initialize()
'Creates a new WMI object
Dim services As SWbemServices
'Creates the callback object
Set sink = New SWbemSink
'Gets the WMI service. The period in the string is used to reference the local computer.
'You may use an IP address if you wish to query a remote computer.
Set services = GetObject("winmgmts:\\.\root\cimv2")
'Executes an asynchronous notification query
services.ExecNotificationQueryAsync sink, "Select * from Win32_PowerManagementEvent"
End Sub
形成
Option Compare Database
Option Explicit
Dim sinker As Class1
Private Sub Form_Load()
Set sinker = New Class1
End Sub
样本查询
查询以返回有关该设备是本地驱动器的存储设备的信息。
SELECT *
FROM Win32_LogicalDisk
WHERE DriveType = 3
查询以返回有关CPU的信息。
SELECT *
FROM Win32_Processor
在本地/远程计算机上创建进程的代码。
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!//./root/cimv2:Win32_Process")
objWMI.Create "C:\Windows\system32\mstsc.exe", Null, Null, intProcID
用于根据进程ID终止进程的代码。
Set objAppList = objWMI.ExecQuery("Select * from Win32_Process where ProcessID = '"& strID &"'",,48)
For Each objApp In objAppList
objApp.Terminate
Next
查询以ping计算机。
SELECT *
FROM Win32_PingStatus
WHERE Address = '" & sAddress & "'"
创建进程时的通知查询。
WITHIN 1是一个轮询间隔。
SELECT *
FROM __InstanceCreationEvent
WITHIN 1
WHERE TargetInstance ISA 'Win32_Process'
通知查询,用于查询网络连接状态何时发生变化。
例如,当网络共享上升或下降时。
SELECT *
FROM __InstanceModificationEvent
WITHIN 1
WHERE Targetinstance ISA 'Win32_NetworkConnection'
翻译自: https://bytes.com/topic/access/insights/942725-windows-management-instrumentation-wmi-tutorial
windows wmi