适用于任何USB设备的WmiEvent


1

我正在关注这个答案,其中包含

Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange

$newEvent = Wait-Event -SourceIdentifier volumeChange

当我插入或卸下USB闪存驱动器时,此方法有效。(我想是因为VolumeChange)我想检测任何 USB设备的插入或移除,而不仅仅是驱动器。这可能吗?谢谢!

Answers:


1

是的,有可能,您必须使用DeviceChangeEvent而不是VolumeChangeEvent

Register-WmiEvent -Class win32_DeviceChangeEvent -SourceIdentifier deviceChange

$newEvent = Wait-Event -SourceIdentifier deviceChange

请注意,SourceIdentifier只是事件订阅的名称,因此您可以根据需要保留“ volumeChange”。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.