USE WMI and WMQ to get Logical Disk and Partition Information from Win32_PnPEntity of SERVICE type USBSTOR
I am detecting when our proprietary USB device is attached to a PC. When we detect this, we get the related Win32_PnPEntity. We want to use this Win32_PnPEntity as a source of the following information:
- USB Descriptor VID, PID, REV, Firmware Version. - Logical Disk Drive Volume and Partition information to get Drive Letter information.The problem I am having is trying to figure out how to use WMI and WMQ to get to the Logical Disk and partition information from the Win32_PnPEntity. The Win32_PnPEntity is of Service type USBSTOR (USB Storage I think?)
Following is a description of the process I currently go through:
I listen to the Win32_USBControllerDevice with the event watcher so as to detect when the USB Device is attached or detached from the PC. I use the following query to constrain what comes through on the event:
"SELECT * FROM
__InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_USBControllerDevice' AND TargetInstance.__RELPATH LIKE '%DeviceId=%VID_1111%'"
When this event comes through I pull the "Win32_USBControllerDevice.Dependent" property and pull out the "DeviceId" value. I then use this "DeviceId" value with the Win32_PNPEntity class to get the associated instance.
This is where things get tricky for me...
I need to get to the Win32_DiskDrive that the Win32_PnPEntity instance relates to. I need this instance so as to get to the volume information, so I can get the drive letter each partition is assigned. But I have no idea on how to get to it.
I go through this process as the Win32_PnPEntity provides the VID, PID for constraint on event listening and searching... but it also provides the Firmware Version which I need. So from this point I want to get the Win32_DiskDrive for the Win32_PnPEntity.
Any input would be appreciated.
--- EXTRA INFORMATION TO BETTER DESCRIBE THE ISSUE:
The steps I have got working so far are as follows:
- Listen to the '__InstanceModificaitionEvent' for the 'Win32_USBControllerDevice' - Get the 'Win32_PnPEntity' from the 'TargetInstnace' of the '__InstanceModificaitionEvent' - Get the 'DeviceId' from the 'Win32_PnPEntity' which contains the PID, VID information.The issue I have now is trying to find the 'Win32_LogicalDisk' AND/OR 'Win32_DiskDrive' AND/OR 'Win32_DiskPartition' OR some other ASSOCIATOR or REFERENCES link to the drive letter the USB device attached has been assigned for the 'Mass Storge' instance.
The best way I can think of the issue is described in the following 'diagram':
USB DEVICE
| |
----------| |----
| Win32_DiskDrive | Win32_PnPEntity
What I am trying to do, is figure out how to get from the 'Win32_PnPEntity' representation of the USB Device to the 'Win32_DiskDrive' representation.
Following is a powershell script I run, while the device is attached. It gives me all the instances that belong to the USB Device. I know they belong to the one device as I have attached only one of the devices and with our custom editing of the USB DESCRIPTOR I know what to expect.
PS C:\Users\USER.DOMAIN> gwmi Win32_PNPEntity |? { ($_.DeviceId -match 'VEN_MYCOMPANY&') -or ($_.DeviceId -match 'VID_XXXX')}
__GENUS : 2
__CLASS : Win32_PnPEntity __SUPERCLASS : CIM_LogicalDevice __DYNASTY : CIM_ManagedSystemElement __RELPATH : Win32_PnPEntity.DeviceID="USB\VID_XXXX&PID_0001\YYYY" __PROPERTY_COUNT : 24 __DERIVATION : {CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement} __SERVER : BYDEFAULT __NAMESPACE : root\cimv2 __PATH : \BYDEFAULT\root\cimv2:Win32_PnPEntity.DeviceID="USB\VID_XXXX&PID_0001\YYYY" Availability : Caption : USB Mass Storage Device ClassGuid : {36fc9e60-c465-11cf-8056-444553540000} CompatibleID : {USB\Class_08&SubClass_06&Prot_50, USB\Class_08&SubClass_06, USB\Class_08} ConfigManagerErrorCode : 0 ConfigManagerUserConfig : False CreationClassName : Win32_PnPEntity Description : USB Mass Storage Device DeviceID : USB\VID_XXXX&PID_0001\YYYY ErrorCleared : ErrorDescription : HardwareID : {USB\VID_XXXX&PID_0001&REV_0001, USB\VID_XXXX&PID_0001} InstallDate : LastErrorCode : Manufacturer : Compatible USB storage device Name : USB Mass Storage Device PNPDeviceID : USB\VID_XXXX&PID_0001\YYYY PowerManagementCapabilities : PowerManagementSupported : Service : USBSTOR Status : OK StatusInfo : SystemCreationClassName : Win32_ComputerSystem SystemName : BYDEFAULT__GENUS : 2
__CLASS : Win32_PnPEntity __SUPERCLASS : CIM_LogicalDevice __DYNASTY : CIM_ManagedSystemElement __RELPATH : Win32_PnPEntity.DeviceID="WPDBUSENUMROOT\UMB\2&37C186B&0&STORAGE#VOLUME#_??_USBSTOR#DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678#YYYY&0#" __PROPERTY_COUNT : 24 __DERIVATION : {CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement} __SERVER : BYDEFAULT __NAMESPACE : root\cimv2 __PATH : \BYDEFAULT\root\cimv2:Win32_PnPEntity.DeviceID="WPDBUSENUMROOT\UMB\2&37C186B&0&STORAGE#VOLUME#_??_USBSTOR#DISK&VEN_MYCOMPANYI&PROD_ZZZZ_-_STORAGE&REV_5678#YYYY&0#" Availability : Caption : ZZZZ ClassGuid : {eec5ad98-8080-425f-922a-dabf3de3f69a} CompatibleID : {wpdbusenum\fs} ConfigManagerErrorCode : 10 ConfigManagerUserConfig : False CreationClassName : Win32_PnPEntity Description : MYCOMPANY - Storage DeviceID : WPDBUSENUMROOT\UMB\2&37C186B&0&STORAGE#VOLUME#_??_USBSTOR#DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678#YYYY&0# ErrorCleared : ErrorDescription : HardwareID : InstallDate : LastErrorCode : Manufacturer : MYCOMPANY Name : ZZZZ PNPDeviceID : WPDBUSENUMROOT\UMB\2&37C186B&0&STORAGE#VOLUME#_??_USBSTOR#DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678#YYYY&0# PowerManagementCapabilities : PowerManagementSupported : Service : WUDFRd Status : Error StatusInfo : SystemCreationClassName : Win32_ComputerSystem SystemName : BYDEFAULT__GENUS : 2
__CLASS 开发者_高级运维 : Win32_PnPEntity __SUPERCLASS : CIM_LogicalDevice __DYNASTY : CIM_ManagedSystemElement __RELPATH : Win32_PnPEntity.DeviceID="STORAGE\VOLUME\_??_USBSTOR#DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678#YYYY&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}" __PROPERTY_COUNT : 24 __DERIVATION : {CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement} __SERVER : BYDEFAULT __NAMESPACE : root\cimv2 __PATH : \BYDEFAULT\root\cimv2:Win32_PnPEntity.DeviceID="STORAGE\VOLUME\_??_USBSTOR#DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678#YYYY&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B}" Availability : Caption : Generic volume ClassGuid : {71a27cdd-812a-11d0-bec7-08002be2092f} CompatibleID : ConfigManagerErrorCode : 0 ConfigManagerUserConfig : False CreationClassName : Win32_PnPEntity Description : Generic volume DeviceID : STORAGE\VOLUME_??_USBSTOR#DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678#YYYY&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B} ErrorCleared : ErrorDescription : HardwareID : {STORAGE\Volume} InstallDate : LastErrorCode : Manufacturer : Microsoft Name : Generic volume PNPDeviceID : STORAGE\VOLUME_??_USBSTOR#DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678#YYYY&0#{53F56307-B6BF-11D0-94F2-00A0C91EFB8B} PowerManagementCapabilities : PowerManagementSupported : Service : volsnap Status : OK StatusInfo : SystemCreationClassName : Win32_ComputerSystem SystemName : BYDEFAULTGENUS : 2 __CLASS : Win32_PnPEntity __SUPERCLASS : CIM_LogicalDevice __DYNASTY : CIM_ManagedSystemElement __RELPATH : Win32_PnPEntity.DeviceID="USBSTOR\DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678\YYYY&0" __PROPERTY_COUNT : 24 __DERIVATION : {CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement} __SERVER : BYDEFAULT __NAMESPACE : root\cimv2 __PATH : \BYDEFAULT\root\cimv2:Win32_PnPEntity.DeviceID="USBSTOR\DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678\YYYY&0" Availability : Caption : MYCOMPANY ZZZZ - Storage USB Device ClassGuid : {4d36e967-e325-11ce-bfc1-08002be10318} CompatibleID : {USBSTOR\Disk, USBSTOR\RAW} ConfigManagerErrorCode : 0 ConfigManagerUserConfig : False CreationClassName : Win32_PnPEntity Description : Disk drive DeviceID : USBSTOR\DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678\YYYY&0 ErrorCleared : ErrorDescription : HardwareID : {USBSTOR\DiskMYCOMPANY___ZZZZ_-_Storage5678, USBSTOR\DiskMYCOMPANY___ZZZZ_-Storage, USBSTOR\DiskMYCOMPANY, USBSTOR\MYCOMPANY___ZZZZ_-_Storage5...}
InstallDate : LastErrorCode : Manufacturer : (Standard disk drives) Name : MYCOMPANY ZZZZ - Storage USB Device PNPDeviceID : USBSTOR\DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678\YYYY&0 PowerManagementCapabilities : PowerManagementSupported : Service : disk Status : OK StatusInfo : SystemCreationClassName : Win32_ComputerSystem SystemName : BYDEFAULTThe above result set shows a lot of information.
Result 1 - 'SERVICE TYPE' is 'USBSTOR'. This is the Win32_PnPEntity I want to use when listening to '__InstanceModificaitionEvent' as it gives me the VID and PID.
Result 2 - Is the Composite Device representation which I am not interested in. Result 3 - 'SERVICE TYPE' is 'volsnap'. This is the volume entity and I am interested in this. Result 4 - 'SERVICE TYPE' is 'disk'. This is the disk entity and I am interested in this.How do I get the relationship between 'Result 1' and 'Result 3' AND/OR 'Result 4'?
I have tried using the 'Win32_PnPDevice' associating class but it does not seem to get me to the point I need. I am new to this powershell WMI and WMQ environment and so it is quite possible I am not using the 'Win32_PnPDevice' correctly.
I ran the following query to identify what was on the' SameElement' and 'SystemElement' properties of the relevant 'Win32_PnPDevice' entity:
gwmi Win32_PNPDevice |%{[wmi]
gwmi Win32_PNPDevice |%{[wmi](
$.SameElement)
} | select {$.Name, $.Description, $.DeviceId, $_.Status}(
$.SystemElement)
} | select {$.Name, $.Description, $.DeviceId, $_.Status}
The results can be read as follows:
Win32_PNPDevice.SameElement - {PHYSICALDRIVE1, Disk drive, PHYSICALDRIVE1, OK} Win32_PNPDevice.SystemElement - {MYCOMPANY ZZZZ - Storage USB Device, Disk drive, USBSTOR_DISK&VEN_MYCOMPANY&PROD_ZZZZ_-_STORAGE&REV_5678_YYYY&0, OK}
Win32_PNPDevice.SameElement - {USB Mass Storage Device, USB Mass Storage Device, USB_VID_XXXX&PID_0001_YYYY, OK}
Win32_PNPDevice.SystemElement - {USB Mass Storage Device, USB Mass Storage Device, USB_VID_XXXX&PID_0001_YYYY, OK}
Maybe I am using the 'Win32_PnPDevice' class incorrectly but it would seem that I can not connect the 'Win32_PnPEntity' to the 'Win32_DiskDrive'...
I hope this extra information better describes my issue.
精彩评论