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程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01