2016-04-27 19 views
0

私は小さなps1スクリプトを書いてキューをクリアし、エンドユーザーが選択したプリンタでスプールを再開します。私が今までに持っているのは、特定のマシンのデフォルトプリンタで意図されていたものですが、どのプリンタが問題になっているのかを正確に選択できるようにする必要があります。現在の機能スクリプトは次のとおりです。テストページを指定されたプリンタに印刷するにはどうすればよいですか?

net stop spooler 

Remove-Item C:\Windows\System32\spool\PRINTERS\* -Force 

net start spooler 

$printer = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true" 

$PrintTestPage = $printer.PrintTestPage() 

$wshell = New-Object -ComObject Wscript.Shell 

$wshell.Popup("I found a problem that I was able to fix. Please try to print again.",0,"Printer Helper",0x1) 

私は、次のようにしてユーザーの入力を許可するようにスクリプトを変更しようとしました。

net stop spooler 

Remove-Item C:\Windows\System32\spool\PRINTERS\* -Force 

net start spooler 

get-printer 

$printer = Read-Host -Prompt 'Please Type In The Name Of The Printer That You Are Having Problems With' 

$PrintTestPage = $printer.PrintTestPage() 

$wshell = New-Object -ComObject Wscript.Shell 

$wshell.Popup("I found a problem that I was able to fix. Please try to print again.",0,"Printer Helper",0x1) 

ただし、次のエラーが発生します。

[System.String]に 'PrintTestPage'という名前のメソッドが含まれていないため、メソッドの呼び出しに失敗しました。

どうすれば対処できますか?

答えて

3

Read-Hostは、Win32_Printer wmiクラスのインスタンスではなく、文字列を返します。

あなたは、インスタンスを取得するためにRead-Hostからの入力を使用することができます。

$PrinterName = Read-Host 'Please Type In The Name Of The Printer That You Are Having Problems With' 

$PrinterInstance = [wmi]"\\.\root\cimv2:Win32_Printer.DeviceID='$PrinterName'" 

# Now you can call PrintTestPage() 
$PrinterInstance.PrintTestPage() 
+1

感謝を!これは美しく動作します。 –

関連する問題