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.
Asked
Active
Viewed 1,238 times
1 Answers
0
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

postanote
- 15,138
- 2
- 14
- 25