WMI Query based on antecedent string

Posted on


I am trying to map PHYSICAL Disks to mounted partitions and even further into mounted volume letters (C: is mounted on partition 0 of disk 0)

First I’m using

 Select * from Win32_DiskDrive

to get the Index for all physical drives. Then I use

 SELECT Name FROM Win32_DiskPartition WHERE Index = " & objDiskDrive.Index

The name is something in the form of Disk #0 Partition #0. Using this I try to run a query against Win32_LogicalDiskToPartition This returns things like:

Antecedent: \COMP_NAMErootcimv2:Win32_DiskPartition.DeviceID="Disk #0, Partition #0"
Dependent: \COMP_NAMErootcimv2:Win32_LogicalDisk.DeviceID="C:"
EndingAddress: 160039272959
StartingAddress: 1048576

If I could query against the antecedent string and parse out the dependent string I could get my mapping, but that is not working. The query is invalid. I am trying to run the following:

   Select * from Win32_LogicalDiskToPartition WHERE Antecedent LIKE '%Disk #0, Partition #0%'


I believe you should use ASSOCIATORS OF statement (reference) to get results you need.
So the wql expression would be:
"ASSOCIATORS OF {Win32_DiskPartition.DeviceID='Disk #0, Partition #0'} WHERE AssocClass = Win32_LogicalDiskToPartition"

You could also use the same technique to get partition(s) first (with assoc class Win32_DiskDriveToDiskPartition)

There is a code snippet published by Microsoft which does exactly what you need (last one: ‘detect which drive letter is associated with a logical disk partition’, which I in fact based my answer on) http://msdn.microsoft.com/en-us/library/aa394592%28v=vs.85%29.aspx

Hope this helps

Leave a Reply

Your email address will not be published.