Powershell WMI 触发器 - 插入特定设备时的操作
Powershell WMI trigger - action when specific device is plugged in
我想在插入特定 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
我应该如何处理事件部分?
有没有办法把它写成像"When the instance containing that Caption exists...do this"?
我正在尝试:
$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
但是当我plug/unplug它时什么也没有发生。
我试验过:
$query = "SELECT * FROM win32_DeviceChangeEvent"
Register-WMIEvent -Query $query -Action {Write-Host "ALERT"}
这项工作有效,但它会在任何设备为 connected/disconnected 时触发。我希望能够仅隔离带有 Lexar 字幕的设备。
非常感谢。
网上有很多关于 WMI 事件的文档,但对于此类事件并没有真正明确的说明,所以这就是我如何让它工作的,我相信它会对其他许多人有用。
您要做的是在设备实例创建和删除时注册一个事件。 (在我们的例子中,这是 PLUGGED IN 和 UNPLUGGED 的同义词)
因此您必须首先找到插入设备时创建的实例的 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 正在创建错误,所以我使用了带有 WQL 通配符“%”的部分字符串(使用:像 USBSTOR%LEXAR%AAEDZZ5RVJ47QS4K% 匹配好的 DeviceID 而不必使用整个字符串)
WMI 事件是很强大的东西!享受吧!
我想在插入特定 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
我应该如何处理事件部分?
有没有办法把它写成像"When the instance containing that Caption exists...do this"?
我正在尝试:
$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
但是当我plug/unplug它时什么也没有发生。
我试验过:
$query = "SELECT * FROM win32_DeviceChangeEvent"
Register-WMIEvent -Query $query -Action {Write-Host "ALERT"}
这项工作有效,但它会在任何设备为 connected/disconnected 时触发。我希望能够仅隔离带有 Lexar 字幕的设备。
非常感谢。
网上有很多关于 WMI 事件的文档,但对于此类事件并没有真正明确的说明,所以这就是我如何让它工作的,我相信它会对其他许多人有用。
您要做的是在设备实例创建和删除时注册一个事件。 (在我们的例子中,这是 PLUGGED IN 和 UNPLUGGED 的同义词)
因此您必须首先找到插入设备时创建的实例的 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 正在创建错误,所以我使用了带有 WQL 通配符“%”的部分字符串(使用:像 USBSTOR%LEXAR%AAEDZZ5RVJ47QS4K% 匹配好的 DeviceID 而不必使用整个字符串)
WMI 事件是很强大的东西!享受吧!