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
The serial number is in Win32_DiskDrive:
SELECT DeviceID, SerialNumber FROM Win32_DiskDrive
Win32_DiskDriveToDiskPartition
has the mapping of Win32_DiskDrive
to Win32_DiskPartition
.
They are mapped Win32_DiskDrive.DeviceID
to Win32_DiskPartition.DeviceID
in Win32_DiskDriveToDiskPartition
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 doesn't support the
JOIN
clause. You need to use theASSOCIATORS OF
statement as you guessed. Here's an example in VBScript: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.Edit: (reply to comment) Yes, you can add a nested
ASSOCIATORS OF
query to get theWin32_PhysicalMedia
instances corresponding to theWin32_DiskDrive
instances; something like this: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.
Here is the C++ code that does the same thing as the VBScript code posted by Helen.