How to join in a WMI Query (WQL)(如何加入 WMI 查询 (WQL))
问题描述
我想通过 WQL 查询获取启动硬盘的序列号.
I want to get the serial number of the boot-harddisk via a WQL query.
可以使用以下查询检索引导分区:
The boot-partition can be retrieved using the following query:
SELECT * FROM Win32_DiskPartition where BootPartition=True
序列号在Win32_DiskDrive:
The serial number is in Win32_DiskDrive:
SELECT DeviceID, SerialNumber FROM Win32_DiskDrive
Win32_DiskDriveToDiskPartition
具有 Win32_DiskDrive
到 Win32_DiskPartition
的映射.它们在 Win32_DiskDriveToDiskPartition
Win32_DiskDriveToDiskPartition
has the mapping of Win32_DiskDrive
to Win32_DiskPartition
.
They are mapped Win32_DiskDrive.DeviceID
to Win32_DiskPartition.DeviceID
in Win32_DiskDriveToDiskPartition
如何构建内部连接 Win32_DiskPartition
和 Win32_DiskDrive
的 WQL 查询?我必须使用关联器还是它可以与内部联接一起使用?
How can I build a WQL query that inner joins Win32_DiskPartition
and Win32_DiskDrive
?
Do I have to use Associators or does it work with INNER JOIN?
推荐答案
WQL 不支持 JOIN
子句.您需要按照您的猜测使用 ASSOCIATORS OF
语句.这是 VBScript 中的示例:
WQL doesn't support the JOIN
clause. You need to use the ASSOCIATORS OF
statement as you guessed. Here's an example in VBScript:
strComputer = "."
Set oWMI = GetObject("winmgmts:\" & strComputer & "
ootCIMV2")
Set colPartitions = oWMI.ExecQuery( _
"SELECT * FROM Win32_DiskPartition WHERE BootPartition=True")
For Each oPartition in colPartitions
Set colDrives = oWMI.ExecQuery( _
"ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& oPartition.DeviceID & "'} WHERE ResultClass=Win32_DiskDrive")
For Each oDrive in colDrives
WScript.Echo oDrive.SerialNumber
Next
Next
但是请注意,Win32_DiskDrive.SerialNumber
属性在 Windows Vista 之前不可用.因此,如果您希望您的代码也适用于较早的 Windows 版本(例如 Windows XP 或 Windows 2000),您应该考虑使用 WMI 以外的 API.
Note, however, that the Win32_DiskDrive.SerialNumber
property isn't available prior to Windows Vista. So, if you want your code to work on earlier Windows versions as well (e.g. Windows XP or Windows 2000) you should consider using APIs other than WMI.
(回复评论) 是的,您可以添加嵌套的 ASSOCIATORS OF
查询以获取 Win32_PhysicalMedia
Win32_DiskDrive
实例对应的 code> 实例;像这样:
(reply to comment) Yes, you can add a nested ASSOCIATORS OF
query to get the Win32_PhysicalMedia
instances corresponding to the Win32_DiskDrive
instances; something like this:
...
For Each oDrive in colDrives
Set colMedia = oWMI.ExecQuery( _
"ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" _
& oDrive.DeviceID & "'} WHERE ResultClass=Win32_PhysicalMedia")
For Each oMedia in colMedia
WScript.Echo oMedia.SerialNumber
Next
Next
您还没有说明您使用的是哪种语言 - 我想在 PowerShell 或 C# 中可以更优雅地完成整个事情,但 VBScript 非常冗长.
You haven't said what language you're using - I guess in PowerShell or C# the whole thing can be done more elegantly, but VBScript is pretty verbose.
这篇关于如何加入 WMI 查询 (WQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何加入 WMI 查询 (WQL)
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01