【问题标题】:Check Printer Status in PowerShell在 PowerShell 中检查打印机状态
【发布时间】:2021-07-07 22:33:23
【问题描述】:

尝试检测是否插入了 USB 打印机,但 Get-Printer 工具可能是错误的或未获取正确的数据。有什么我想念的吗?请注意,当我在 .NET 应用程序中获取数据时,我也会看到此问题。

【问题讨论】:

    标签: powershell printing usb printers


    【解决方案1】:

    您不能只使用自己提到的 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 插入的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多