PoshCode Logo PowerShell Code Repository

Get pages printed by KevMar 3 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/5179"></script>download | new post

It parses the print log file for printer events related to printing. It is important that the log be enabled. If a log is cleared, then this data will be inaccurate.

  1.  
  2. <#
  3. .Synopsis
  4.    Pulls the printer history from the target computer.
  5. .DESCRIPTION
  6.    It parses the print log file for printer events related to printing. It is important that the log be enabled. If a log is cleared, then this data will be inaccurate.
  7. .PARAMETER computername
  8. The computer name(s) to retrieve the info from.
  9. .EXAMPLE
  10.    Get-PrintHistory
  11. .EXAMPLE
  12.    Get-PrintHistory -ComputerName localhost
  13. #>
  14. function Get-PrintHistory
  15. {
  16.     [CmdletBinding()]
  17.     [OutputType([object])]
  18.     Param
  19.     (
  20.         # Param1 help description
  21.         [Parameter(Mandatory=$false,
  22.                    ValueFromPipelineByPropertyName=$true,
  23.                    Position=0)]
  24.         [string]$ComputerName="localhost"
  25.  
  26.     )
  27.  
  28.     Begin
  29.     {
  30.     }
  31.     Process
  32.     {
  33.         Write-Verbose "Getting Events from Computer: $ComputerName"
  34.         $log = Get-WinEvent -FilterHashTable @{ "LogName"= "Microsoft-Windows-PrintService/Operational";"ID"="307"} -ComputerName $ComputerName
  35.      
  36.         $MessageRegEx = "(?<Document>.+), Print Document owned by (?<Username>.+) on (?<Computer>.+) was printed on (?<Printer>.+) through port (?<IP>.+)\.  Size in bytes: (?<Size>\d+)\. Pages printed: (?<Pages>\d+)\. No user action is required\."
  37.         Write-Verbose "Parsing $($log.Count) events"
  38.         $log | ?{$_.message -match $MessageRegEx} |
  39.             %{ New-Object PSObject -property @{"Document"=$Matches.Document;
  40.             "UserName"=$Matches.Username;
  41.             "IP"=$Matches.IP;
  42.             "ComputerName"=$Matches.Computer;
  43.             "Pages"=$Matches.Pages;
  44.             "TimeStamp"= $_.TimeCreated;
  45.             "Printer" = $Matches.Printer;
  46.             "PrintHost" = $_.MachineName
  47.             }}
  48.  
  49.     }
  50.     End
  51.     {
  52.         Write-Verbose "Done processesing print events"
  53.     }
  54. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me