我正在编写一个小的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'的方法。
我该如何解决这个问题?
答案 0 :(得分: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()