如何加入 WMI 查询 (WQL)

How to join in a WMI Query (WQL)(如何加入 WMI 查询 (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_DiskDriveWin32_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_DiskPartitionWin32_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_PhysicalMediaWin32_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)

基础教程推荐