Powershellでプリンタの状態(ステータス)を取得するにはPrintManagementモジュールのGet-Printerを利用すればよい。
必要な環境
Powershll 3.0 以降
実行例
Get-Printerコマンドレットで特定のプリンタ名を指定してでCIMインスタンス*1を取得し、PrinterStatusを取得する*2。Normalなら利用できる状態のようだ。
PS D:\SandBox> (Get-Printer –Name "Microsoft XPS Document Writer").PrinterStatus Normal
※実行環境は以下
PS D:\SandBox> "$((get-wmiobject win32_operatingsystem).caption) ($((get-wmiobject win32_operatingsystem).version))" Microsoft Windows 10 Pro (10.0.14393) PS D:\SandBox> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 5 1 14393 206
参考
PrintManagementモジュールについて
- PowerShell で プリンターのポート操作と紐づけを行ってみる - tech.guitarrapc.cóm
- Use PowerShell to Test Remote Printers – Hey, Scripting Guy! Blog
CIMについて
- Windows用語集 - WMI:ITpro CIM(Common Information Model)はWIM(Windows Management Instrumentation)の一部で監視情報のデータ構造を標準化するための形式、と理解した。
Powershellをバージョン指定で実行する方法について
- 最新OS上で古いバージョンのPowerShellを実行する - YOMON8.NET 動作確認したいときに重宝する
余談
いつもの通り、英語版も含めてPowershellのレファレンスはクソなので何とかしてほしい。 PrinterStatusが取りうる値の範囲ぐらい明記してほしい。