【问题标题】:Need to get file info before it will be printed using PowerShell需要在使用 PowerShell 打印之前获取文件信息
【发布时间】:2019-04-29 10:24:01
【问题描述】:

我需要获取已发送到打印机的文件的名称和大小。所以我需要在打印文件之前获取有关它的信息。

我尝试使用 Windows\System32\spool\PRINTERS 中的文件,但即使我暂停打印工作,我也无法从 .SHD 和 .SPL 文件中获取任何信息。

我开始寻找一些使用 Get-WmiObject -Class Win32_Printer 的解决方案。 这是一个正确的方法吗?也许我应该使用一些特定的方法或其他东西?

我试过这段代码,但显示错误

$comp = $(Get-WmiObject Win32_Computersystem).Name
if ( (Get-ChildItem C:\Windows\System32\spool\PRINTERS | Measure-Object).Count -ne 0)
{
  Get-WmiObject -Class win32_service -filter 'name="spooler"' -ComputerName $comp | Invoke-WmiMethod -Name StopService | out-null
  $name = $(Get-WmiObject Win32_PrintJob).Document
  $size = $(Get-WmiObject Win32_PrintJob).Size
  $time = $(Get-WmiObject Win32_PrintJob).StartTime
  "$comp,$name,$size,$time" | Out-file C:\Scripts\PrintJobs.csv -Append
  Set-Service spooler -ComputerName $comp -Status Running
}

怎么了?

PowerShell 对我来说是一个新事物,现在我完全迷失了这项任务

【问题讨论】:

    标签: powershell powershell-3.0


    【解决方案1】:

    除非您知道何时运行 powershell,否则我看不到您如何在发送打印作业“之前”获取此信息。您必须保持后台处理程序“暂停”,然后在脚本运行时将其设置为正在运行,然后再次暂停,直到下次运行脚本。

    有一件事是“Get-WmiObject Win32_PrintJob”很可能会返回所有当前假脱机作业的集合。所以你的代码应该看起来像这样来获取你正在寻找的信息:

    $comp = $(Get-WmiObject Win32_Computersystem).Name
    $jobs=Get-WmiObject Win32_PrintJob
    $jobInfo = $jobs | ForEach-Object {"$comp,$($_.Document),$($_.Size),$($_.StartTime)"}
    $jobInfo | Out-file C:\Scripts\PrintJobs.csv -Append
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      相关资源
      最近更新 更多