PowerShell事件注册:Action块执行多次 [英] PowerShell Event Registration: Action block executes multiple times

查看:198
本文介绍了PowerShell事件注册:Action块执行多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:每次插入USB时,-Action {}内的代码运行6-7次

所需:我只想在事件发生时处理它一次。



示例:

$ query =SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2

Register-WMIEvent -Query $ query -Action {Write-HostUSB Plug in In} -SourceIdentifierPluggedIn



在这种情况下,我会写USB Plug in In6我插入1 usb一次的时间。不确定原因,但每次事件多次提出。至少USB是这种情况。我想用1个动作响应usb插件事件并做1次,而不是6次。需要帮助,谢谢。

解决方案

query =SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2

Register-WMIEvent -Query

query -Action {Write-HostUSB Plug in In} -SourceIdentifierPluggedIn



在这种情况下,我将获得USB插入我插入1 usb一次写入6次。不确定原因,但每次事件多次提出。至少USB是这种情况。我想用1个动作响应usb插件事件并做1次,而不是6次。需要帮助,谢谢。


Problem: every time USB is plugged in, code inside -Action{} runs 6-7 times
Desired: I want to handle it only once when event is raised.

Example:
$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action {Write-Host "USB Plugged In"} -SourceIdentifier "PluggedIn"

In this case I will get "USB Plugged In" written 6 times when I plug 1 usb once. Not sure why, but every time the event is raised multiple times. At least this is the case for USB. I want to respond to usb plug in event with 1 action and do it 1 time, not 6 times. Need help, thanks.

解决方案

query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query


query -Action {Write-Host "USB Plugged In"} -SourceIdentifier "PluggedIn"

In this case I will get "USB Plugged In" written 6 times when I plug 1 usb once. Not sure why, but every time the event is raised multiple times. At least this is the case for USB. I want to respond to usb plug in event with 1 action and do it 1 time, not 6 times. Need help, thanks.


这篇关于PowerShell事件注册:Action块执行多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆