我正在创建一个 PowerShell 函数,该函数执行与命令 Pause Printing 相同的任务,该命令可在打印机“que”对话框窗口的 Printer 菜单中找到。
大多数时候,我需要完全暂停所有打印,但传入的打印作业只是排队,直到我切换暂停打印
虽然PrintManagement模块提供了很多有用的命令,但它缺少这个命令。
到处搜索,我不断遇到 PowerShell 这种徒劳的代码:
Get-WMIObject Win32_PerfFormattedData_Spooler_PrintQueue | Select Name, JobsSpooling, Jobs|where {$_.jobs -gt 0}
(gwmi win32_printer -filter "name='printer_name'").pause()
(gwmi win32_printer -filter "name='printer_name'").resume()
在我的机器上运行第一行不会产生错误:
Get-WMIObject Win32_PerfFormattedData_Spooler_PrintQueue | Select Name, JobsSpooling, Jobs
Name JobsSpooling Jobs
---- ------------ ----
Xerox 0 2
_Total 0 2
但是运行
(gwmi win32_printer -filter "name='printer_name'").pause()
后返回错误:
gwmi: The term 'gwmi' is not recognized as a name of a cmdlet, function, script file,
我认为
gwmi
是 Get-WMIObject
的别名,所以我尝试了 (Get-WmiObject win32_printer -filter "name='printer_name'").pause()
但出现错误:
InvalidOperation: You cannot call a method on a null-valued expression.
甚至将第一行的输出保存到变量中,然后运行其方法:
$obj = Get-WMIObject Win32_PerfFormattedData_Spooler_PrintQueue | Select Name, JobsSpooling, Jobs
$obj.pause()
错误:
InvalidOperation: Method invocation failed because [Selected.System.Management.Automation.PSCustomObject] does not contain a method named 'pause'.
有没有办法真正做到这一点?
编辑1: @杰夫·泽特林
我刚刚为我的打印机命名了
zerox
,当时我设置它时,这也是我传递给get-printerJob
的名称:
Get-PrintJob -PrinterName xerox
Id ComputerName PrinterName DocumentName SubmittedTime JobStatus
-- ------------ ----------- ------------ ------------- ---------
26 Xerox xxxxx.txt - Notepad 28/03/2024 17:22:15 Normal
27 Xerox xxxxx.txt - Notepad 28/03/2024 17:22:19 Normal
我是否需要某种技术名称(型号或其他名称)?我不知道如何得到这个,无论如何我都会开始四处看看。
在 powershell 7 中使用 get-ciminstance。我相信这适用于真正的打印机。
get-ciminstance win32_printer | ? name -eq 'adobe pdf' |
invoke-cimmethod -methodname pause
ReturnValue PSComputerName
----------- --------------
5