Print job logging

Hello,

I’ve been struggling to report on successful and failed print jobs. My bot runs a series of PowerShell Print jobs inside a loop and I’m trying to store the print results in a data table.

I can log and capture successful print jobs with this Powershell script but it doesn’t give me a filename.

Get-WinEvent -FilterHashTable @{LogName=“Microsoft-Windows-PrintService/Operational”; ID=307; StartTime=(Get-Date).AddDays(-1)} |
Format-Table -Property TimeCreated,
@{label=‘UserName’;expression={$.properties[2].value}},
@{label=‘ComputerName’;expression={$
.properties[3].value}},
@{label=‘PrinterName’;expression={$.properties[4].value}},
@{label=‘PrintSize’;expression={$
.properties[6].value}},
@{label=‘Pages’;expression={$_.properties[7].value}}

Can anyone help me with a process that also stores the file name?

Thanks,

Tim

To retrieve the file name associated with a print job, you can modify the PowerShell script to include the file name information. The file name is typically part of the event data, and you can extract it by accessing the appropriate property. Here’s an updated version of your script:

Get-WinEvent -FilterHashTable @{LogName=“Microsoft-Windows-PrintService/Operational”; ID=307; StartTime=(Get-Date).AddDays(-1)} |
ForEach-Object {
$eventData = [xml]$.ToXml()
$fileInfo = $eventData.Event.EventData.Data | Where-Object {$
.Name -eq “DocumentName”}

[PSCustomObject]@{
    TimeCreated = $_.TimeCreated
    UserName = $eventData.Event.EventData.Data | Where-Object {$_.Name -eq "UserName"}.'#text'
    ComputerName = $eventData.Event.EventData.Data | Where-Object {$_.Name -eq "ComputerName"}.'#text'
    PrinterName = $eventData.Event.EventData.Data | Where-Object {$_.Name -eq "PrinterName"}.'#text'
    PrintSize = $eventData.Event.EventData.Data | Where-Object {$_.Name -eq "Size (KB)"}.'#text'
    Pages = $eventData.Event.EventData.Data | Where-Object {$_.Name -eq "Pages"}.'#text'
    FileName = $fileInfo.'#text'
}

} | Format-Table

1 Like