本文介绍了使用WMI和WMQ从SERVICE类型USBSTOR的Win32_PnPEntity获取逻辑磁盘和分区信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检测何时将我们的专有USB设备连接到PC.当我们检测到这一点时,我们得到了相关的"Win32_PnPEntity".我们想将此"Win32_PnPEntity"用作以下信息的来源:
-USB描述符VID,PID,REV,固件版本.
-逻辑磁盘驱动器的卷和分区信息以获取驱动器号信息.

我遇到的问题是试图弄清楚如何使用WMI和WMQ从"Win32_PnPEntity"获取逻辑磁盘和分区信息.

"Win32_PnPEntity"的服务类型为"USBSTOR"(我认为是USB存储吗?)


以下是我目前正在经历的过程的描述:

我使用事件观察器收听"Win32_USBControllerDevice",以检测何时将USB设备连接到PC或从PC上卸下.我使用以下查询来约束事件中发生的事情:


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_XXXX%''"



当此事件发生时,我将拉出"Win32_USBControllerDevice.Dependent"属性,并拉出"DeviceId"值.然后,我将此"DeviceId"值与"Win32_PNPEntity"类一起使用以获取关联的实例.

这对我来说很棘手...

我需要进入与"Win32_PnPEntity"实例相关的"Win32_DiskDrive".我需要这个实例来获取卷信息,因此我可以获得每个分区分配的驱动器号.但是我不知道该怎么去.

我经历了这个过程,因为"Win32_PnPEntity"提供了VID,PID,以限制事件侦听和搜索...但是它还提供了我需要的固件版本.因此,从这一点出发,我想为"Win32_PnPEntity"获取"Win32_DiskDrive".

到目前为止,我已经完成的步骤如下:
-听``Win32_USBControllerDevice''的``__InstanceModificaitionEvent''
-从``__InstanceModificaitionEvent''的``TargetInstnace''中获取``Win32_PnPEntity''
-从"Win32_PnPEntity"中获取"DeviceId",其中包含PID,VID信息.

我现在遇到的问题是试图找到"Win32_LogicalDisk"和/或"Win32_DiskDrive"和/或"Win32_DiskPartition",或指向分配了USB设备的驱动器号的其他一些ASSOCIATOR或REFERENCES链接"Mass Storge"实例.

我的问题是USB设备由"Win32_DiskDrive"和"Win32_PnPEntity"表示,但我不知道如何连接两者.以下是一些更多示例和详细信息,说明了我为尝试连接各个点所做的​​工作.

以下是连接设备后运行的Powershell脚本.它为我提供了属于USB设备的所有实例.我知道它们属于一台设备,因为我仅连接了其中一台设备,并且通过对USB DESCRIPTOR的自定义编辑,我知道会发生什么.



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''.

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 issue I have is that the USB Device is represented by ''Win32_DiskDrive'' and by ''Win32_PnPEntity'' but I do not know how to connect the two. Following are some more examples and detail on what I have done to try and connect the dots.

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 : ZZZZ - 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 : BYDEFAULT

__GENUS : 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 : BYDEFAULT


上面的结果集显示了很多信息.

-结果1:服务类型"为"USBSTOR".这是我在听``__InstanceModificaitionEvent''时要使用的Win32_PnPEntity,因为它为我提供了VID和PID.
-结果2:是我不感兴趣的复合设备表示形式.
-结果3:服务类型"为"volsnap".这是体积实体,对此我很感兴趣.
-结果4:服务类型"为磁盘".这是磁盘实体,对此我很感兴趣.

如何获得结果1"和结果3" AND/OR结果4"之间的关系?

我曾尝试使用"Win32_PnPDevice"关联类,但似乎无法使我达到需要的程度.我是这个Powershell WMI和WMQ环境的新手,因此很可能我没有正确使用"Win32_PnPDevice".

我运行以下查询来确定相关"Win32_PnPDevice"实体的"SameElement"和"SystemElement"属性上的内容:


The 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]($.SameElement)} | select {$.Name, $.Description, $.DeviceId, $_.Status}
gwmi Win32_PNPDevice |%{[wmi]($.SystemElement)} | select {$.Name, $.Description, $.DeviceId, $_.Status} 



结果可以如下所示:
- Win32_PNPDevice.SameElement -{PHYSICALDRIVE1,磁盘驱动器,PHYSICALDRIVE1,确定}
- Win32_PNPDevice.SystemElement -{MYCOMPANY ZZZZ-存储USB设备,磁盘驱动器,USBSTOR_DISK& VEN_MYCOMPANY& PROD_ZZZZ _-_ STORAGE& REV_5678_YYYY& 0,确定}

- Win32_PNPDevice.SameElement -{USB大容量存储设备,USB大容量存储设备,USB_VID_XXXX& PID_0001_YYYY,确定}
- Win32_PNPDevice.SystemElement -{USB大容量存储设备,USB大容量存储设备,USB_VID_XXXX& PID_0001_YYYY,确定}

也许我没有正确使用"Win32_PnPDevice"类,但似乎无法将"Win32_PnPEntity"连接到"Win32_DiskDrive" ...



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''...

推荐答案



上面的结果集显示了很多信息.

-结果1:服务类型"为"USBSTOR".这是我在听``__InstanceModificaitionEvent''时要使用的Win32_PnPEntity,因为它为我提供了VID和PID.
-结果2:是我不感兴趣的复合设备表示形式.
-结果3:服务类型"为"volsnap".这是体积实体,对此我很感兴趣.
-结果4:服务类型"为磁盘".这是磁盘实体,对此我很感兴趣.

如何获得结果1"和结果3" AND/OR结果4"之间的关系?

我曾尝试使用"Win32_PnPDevice"关联类,但似乎无法使我达到需要的程度.我是这个Powershell WMI和WMQ环境的新手,因此很可能我没有正确使用"Win32_PnPDevice".

我运行以下查询来确定相关"Win32_PnPDevice"实体的"SameElement"和"SystemElement"属性上的内容:


The 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](



这篇关于使用WMI和WMQ从SERVICE类型USBSTOR的Win32_PnPEntity获取逻辑磁盘和分区信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:52