I want to get a notification when a usb device is inserted/removed/enabled/disabled.
I am registering to “__InstanceOperationEvent” for Win32_USBControllerDevice.
The problem is that although I get the insert/removed notifications for all the devices.
I get the enabled/disabled events only for USB storage devices.
What am I doing wrong?
Thanks in advance
It’s a little hard to grasp what you are talking about without providing a code sample or telling us what language you are working in.
The
Win32_USBControllerclass is an associator of the Win32_PnPEntity class which does have an instance of all USB devices regardless of type. So an__InstanceOperationEventfor this class DOES provide notifications for all devices, not just USB storage devices. More appropriately, it will work for any PnP-compatible USB device.The following script is a point of concept written in VBScript. It will notify you of all USB components when a device is connected or disconnected. I tested with USB storage devices, USB mouse and keyboard, and various other devices I had lying around. All of them worked. I tested on Vista x64 but this should work with any version of Windows.
vbscript Sample:
Determining when a devices is enabled or disabled is similar but has a few distinct differences. You’ll want to use the
Win32_PnPEntityclass which is basically an enumeration of all installed PnP devices. You’ll want be focusing on theConfigManagerErrorCodeproperty for each instance. This provides contains the same information that you will find in the Device status portion of Device Manager. You can see why it would be useful to watch this propery for any given device. When a device changes to show the code (0) for normal operation, we can safely assume that a device has been enabled. When this code changes to code 22, we know that a devices has been disabled. (All other codes indicate error states.) The__InstanceModificationEventis a good choice to watch for changes in any instances of theWin32_PnPEntityclass.If you’re interested in the possible error codes you can throw in a
WScript.Echostatement near the beginning of the script where it enumerates all of the possible values.vbscript Sample: