简体   繁体   中英

Check Printer Status in PowerShell

Trying to detect when a USB printer is plugged in or not, but the Get-Printer is either the wrong tool or is not getting the correct data. Is there something I am missing? Note, I am also seeing this issue when I and getting the data in a .NET application.

命令输出+打印机状态

打印机的所有状态属性

You cannot just use the cmdlets mentioned by yourself and @Ash to accomplish your use case...

'...detect when a USB printer is plugged in...'

You have to have an active event listening for that.

Example: for a USB disk.

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

Or messaging the user on the event...

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 }

Of course, you need to specify the correct device type.

Or any USB device

Register-WmiEvent -Class win32_DeviceChangeEvent -SourceIdentifier deviceChange
$newEvent = Wait-Event -SourceIdentifier deviceChange

Then immediately query what was inserted via the cmdlets already mentioned by you and @Ash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM