【发布时间】:2021-07-07 22:33:23
【问题描述】:
【问题讨论】:
标签: powershell printing usb printers
【问题讨论】:
标签: powershell printing usb printers
您不能只使用自己提到的 cmdlet 和 @Ash 来完成您的用例...
' ...检测何时插入 USB 打印机...'
你必须有一个活动的事件来监听它。
例如:对于 U 盘。
Register-WmiEvent -Query "Select * from __InstanceCreationEvent within 5 where targetinstance isa 'win32_logicaldisk'" -SourceIdentifier disk -Timeout 1000
Get-Event -SourceIdentifier disk
$DiskInsertEvent = Get-Event -SourceIdentifier disk
$DiskInsertEvent.SourceEventArgs.NewEvent.TargetInstance
或者在事件中向用户发送消息...
Register-WmiEvent -Query "select * from __InstanceCreationEvent within 5 where TargetInstance ISA 'Win32_PnpEntity' and TargetInstance.Description like '%USB Mass Storage Device%'" -Action { Write-Host "Please talk to IT about your USB device." -ForegroundColor Red }
当然,您需要指定正确的设备类型。
或任何 USB 设备
Register-WmiEvent -Class win32_DeviceChangeEvent -SourceIdentifier deviceChange
$newEvent = Wait-Event -SourceIdentifier deviceChange
然后立即查询通过您和@Ash 已经提到的 cmdlet 插入的内容
【讨论】: