如何加入WMI查询(WQL) [英] How to join in a WMI Query (WQL)

查看:231
本文介绍了如何加入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_DiskDrive.DeviceID映射到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查询? 我必须使用关联器,还是可以与INNER JOIN一起使用?

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 & "\root\CIMV2") 

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

但是请注意,在Windows Vista之前,Win32_DiskDrive.SerialNumber属性不可用.因此,如果您希望代码也能在早期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_DiskDrive实例相对应的Win32_PhysicalMedia实例;像这样的东西:

(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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆