import wmi
import win32com.client
def Usb_detect():
raw_wql = "SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA \'Win32_USBHub\'"
c = wmi.WMI ()
watcher = c.watch_for(raw_wql=raw_wql)
# while 1:
usb = watcher()
#<class 'wmi._wmi_event'>
return usb
print(Usb_detect())
OUTPUT:
instance of Win32_USBHub
{
Caption = "USB Composite Device ";
ConfigManagerErrorCode = 0;
ConfigManagerUserConfig = FALSE;
CreationClassName = "Win32_USBHub";
Description = "USB Composite Device ";
DeviceID = "xyz";
Name = "USB Composite Device ";
PNPDeviceID = "xyz";
Status = "OK";
SystemCreationClassName = "Win32_ComputerSystem";
SystemName = "xyz";
};
I want to convert this WMI object to json using python. Please suggest me a solution. I have tried different ways but not able to get a reference.