Querying usb device by DeviceID with WQL

0

I can get deviceID from WMI, then I want to use that deviceID to check weather device is in enabled\disabled state and if it's status is OK or not, basically I want to use WQL to query that device later on using this usb device unique DeviceID. Here is a code example that I used and got exception with

import wmi
devid = "USB\VID_04F2&PID_B315\6&EF94D1A&0&6"
c = wmi.WMI()
q2 = "SELECT * FROM Win32_PnPEntity WHERE DeviceID = " + devid + " "
dev = c.query(q2)

When I run this code I get the following error:

Traceback (most recent call last):
File "", line 1, in
File "C:\Python27\lib\site-packages\wmi.py", line 1009, in query
return [ _wmi_object (obj, instance_of, fields) for obj in self._raw_query(wql) ]
File "C:\Python27\lib\site-packages\win32com\client\util.py", line 84, in next return _get_good_object_(self.iter.next(), resultCLSID = self.resultCLSID)
pywintypes.com_error: (-2147217385, 'OLE error 0x80041017', None, None)

Probably my wql query is wrong somehow, could you give me an example of the right way the compose the query?

python
wmi
wql
asked on Stack Overflow Aug 25, 2016 by user6603026 • edited Aug 26, 2016 by marc_s

1 Answer

0

\ is a special character in WQL and must be escaped with a backslash so your devid should be this:

devid = "'USB\\VID_04F2&PID_B315\\6&EF94D1A&0&6'"

Edit: I also noticed that you aren't wrapping the constant so I added a single quote around the value.

answered on Stack Overflow Aug 25, 2016 by Tim • edited Aug 25, 2016 by Tim

User contributions licensed under CC BY-SA 3.0