我想在插入特定的USB设备时触发脚本,我研究了Register-WmiEvent
,但对于如何正确处理它感到非常困惑。
到目前为止,我已经成功地将设备隔离了:
Get-WmiObject win32_PNPEntity | where {$_.Caption -eq "Lexar USB Flash Drive USB Device"}
这是返回的WMI对象:
_GENUS : 2
__CLASS : Win32_PnPEntity
__SUPERCLASS : CIM_LogicalDevice
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_PnPEntity.DeviceID="USBSTOR\\DISK&VEN_LEXAR&PROD_USB_FLASH_DRIVE&REV_1100\\AAEDZZ5RVJ47QS4K&0"
__PROPERTY_COUNT : 26
__DERIVATION : {CIM_LogicalDevice, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER : P7409
__NAMESPACE : root\cimv2
__PATH : \\P7409\root\cimv2:Win32_PnPEntity.DeviceID="USBSTOR\\DISK&VEN_LEXAR&PROD_USB_FLASH_DRIVE&REV_1100\\AAEDZZ5RVJ47QS4K&0"
Availability :
Caption : Lexar USB Flash Drive USB Device
ClassGuid : {4d36e967-e325-11ce-bfc1-08002be10318}
CompatibleID : {USBSTOR\Disk, USBSTOR\RAW, GenDisk}
ConfigManagerErrorCode : 0
ConfigManagerUserConfig : False
CreationClassName : Win32_PnPEntity
Description : Lecteur de disque
DeviceID : USBSTOR\DISK&VEN_LEXAR&PROD_USB_FLASH_DRIVE&REV_1100\AAEDZZ5RVJ47QS4K&0
ErrorCleared :
ErrorDescription :
HardwareID : {USBSTOR\DiskLexar___USB_Flash_Drive_1100, USBSTOR\DiskLexar___USB_Flash_Drive_, USBSTOR\DiskLexar___, USBSTOR\Lexar___USB_Flash_Drive_1...}
InstallDate :
LastErrorCode :
Manufacturer : (Lecteurs de disque standard)
Name : Lexar USB Flash Drive USB Device
PNPClass : DiskDrive
PNPDeviceID : USBSTOR\DISK&VEN_LEXAR&PROD_USB_FLASH_DRIVE&REV_1100\AAEDZZ5RVJ47QS4K&0
PowerManagementCapabilities :
PowerManagementSupported :
Present : True
Service : disk
Status : OK
StatusInfo :
SystemCreationClassName : Win32_ComputerSystem
SystemName : P7409
PSComputerName : P7409
我应该如何处理活动部分?
有没有一种编写方法,它的工作原理类似于“当包含该Caption的实例存在时...执行此操作” 吗?
我正在尝试:
$query = "Select * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'win32_PNPEntity' and TargetInstance.Caption = 'Lexar USB Flash Drive USB Device'"
Register-WMIEvent -Query $query -Action { Write-Host "LEXAR FLASH DRIVE CONNECTED"} -SourceIdentifier TEST
但是当我插入/拔出它时什么也没发生。
我尝试过:
$query = "SELECT * FROM win32_DeviceChangeEvent"
Register-WMIEvent -Query $query -Action {Write-Host "ALERT"}
此功能,但是在连接/断开任何设备时触发。我希望能够使用Lexar字幕仅隔离该设备。
非常感谢。
答案 0 :(得分:1)
在线上有很多有关WMI事件的文档,但是对于这样的事情并没有真正清除,因此这是我的工作方式,我相信它对许多其他人都会有用。
您要做的是在设备实例被创建和删除时注册一个事件。 (在我们的示例中,这是“插入”和“未插入”的同义词)
所以,你必须先找到,当你插上设备时生成实例的ID。我的是:
Win32_PnPEntity.DeviceID="USBSTOR\\DISK&VEN_LEXAR&PROD_USB_FLASH_DRIVE&REV_1100\\AAEDZZ5RVJ47QS4K&0"
*那个设备ID引起了我的麻烦(我想是因为其中有所有的分隔符),所以我与字符串的一部分而不是整个字符串进行了匹配,效果也一样。
以下是创建两个事件的方法:
#Event when plugged in (InstanceCreationEvent)
$query = "Select * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'win32_PNPEntity' and TargetInstance.DeviceID like 'USBSTOR%LEXAR%AAEDZZ5RVJ47QS4K%'"
Register-WMIEvent -Query $query -Action { msg * lexar connected} -SourceIdentifier LexarConnect
#Event when disconnected (InstanceDeletionEvent)
$query = "Select * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'win32_PNPEntity' and TargetInstance.DeviceID like 'USBSTOR%LEXAR%AAEDZZ5RVJ47QS4K%'"
Register-WMIEvent -Query $query -Action { msg * lexar disconnected} -SourceIdentifier LexarDisconnect
正如我上面所说的,的DeviceID被创建错误,所以I(使用:像USBSTOR%LEXAR%AAEDZZ5RVJ47QS4K%的良好的DeviceID,而不必使用整个字符串匹配)用于与WQL通配符“%”的字符串部分
WMI事件功能强大!享受吧!