Last active
October 31, 2023 19:52
-
-
Save ubergeek42/1ba736a4520b3c8ba498fa34cbf49686 to your computer and use it in GitHub Desktop.
Powershell print logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Printer *MUST* be configured to "Store print jobs". This script will delete them later. | |
# Trigger this script on event 307 and pass in the 5 variables below(See the xml file here for the task scheduler definition) | |
# | |
# JOBID formatted as 5 digit number | |
#create c:\scripts\tmp\JOBID_DIR | |
$JOBID=$args[0] | |
$TIMESTAMP=$args[1] | |
$USER=$args[2] | |
$PRINTER=$args[3] | |
$DOCTITLE=$args[4] | |
$TIMESTAMP=$TIMESTAMP.replace(':','_') | |
# Ignore printers we don't care about | |
if ($PRINTER -ne "YOUR_PRINTER_NAME") { | |
Exit 0 | |
} | |
$OUTDIR="c:\scripts\pdf_prints\$PRINTER" | |
New-Item -Force -Path "$OUTDIR" -ItemType directory | |
# Create a copy of the SPL file from the print spooler | |
$SPLFile = "$OUTDIR\$TIMESTAMP-$USER-$JOBID.spl" | |
$SOURCEFILE="c:\windows\system32\spool\printers\" + $JOBID.ToString("00000") + ".spl" | |
#write-host $SPLFile | |
#write-host $SOURCEFILE | |
Copy-Item "$SOURCEFILE" "$SPLFile" | |
Write-Host "Creating thumbnail for this job" | |
# run ghostscript to make a pdf(Only caring about first 10 pages) | |
c:\scripts\ghostpcl\gpcl6win64-9.18.exe -o "$OUTDIR\$TIMESTAMP-$USER-$JOBID.pdf" -dFirstPage=1 -dLastPage=10 -sDEVICE=pdfwrite $SPLFile | |
# delete the print job | |
Write-Host "Deleting print job $JOBID" | |
wmic printjob where jobid=$JOBID delete |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
powershell.exe -command c:\scripts\archive_pdf.ps1 "%1" "%2" "%3" "%4" "%5" >> c:\scripts\archive_pdf.log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-16"?> | |
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> | |
<RegistrationInfo> | |
<Date>2016-02-29T13:35:48.3364142</Date> | |
<Author>ubergeek42</Author> | |
</RegistrationInfo> | |
<Triggers> | |
<EventTrigger> | |
<Enabled>true</Enabled> | |
<Subscription><QueryList><Query Id="0" Path="Microsoft-Windows-PrintService/Operational"><Select Path="Microsoft-Windows-PrintService/Operational">*[System[Provider[@Name='Microsoft-Windows-PrintService'] and EventID=307]]</Select></Query></QueryList></Subscription> | |
<ValueQueries> | |
<Value name="doctitle">Event/UserData/DocumentPrinted/Param2</Value> | |
<Value name="jobid">Event/UserData/DocumentPrinted/Param1</Value> | |
<Value name="printername">Event/UserData/DocumentPrinted/Param5</Value> | |
<Value name="timestamp">Event/System/TimeCreated/@SystemTime</Value> | |
<Value name="username">Event/UserData/DocumentPrinted/Param3</Value> | |
</ValueQueries> | |
</EventTrigger> | |
</Triggers> | |
<Principals> | |
<Principal id="Author"> | |
<UserId>S-1-5-18</UserId> | |
<RunLevel>LeastPrivilege</RunLevel> | |
</Principal> | |
</Principals> | |
<Settings> | |
<MultipleInstancesPolicy>Queue</MultipleInstancesPolicy> | |
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries> | |
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> | |
<AllowHardTerminate>true</AllowHardTerminate> | |
<StartWhenAvailable>false</StartWhenAvailable> | |
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> | |
<IdleSettings> | |
<StopOnIdleEnd>true</StopOnIdleEnd> | |
<RestartOnIdle>false</RestartOnIdle> | |
</IdleSettings> | |
<AllowStartOnDemand>true</AllowStartOnDemand> | |
<Enabled>true</Enabled> | |
<Hidden>false</Hidden> | |
<RunOnlyIfIdle>false</RunOnlyIfIdle> | |
<WakeToRun>false</WakeToRun> | |
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit> | |
<Priority>7</Priority> | |
</Settings> | |
<Actions Context="Author"> | |
<Exec> | |
<Command>c:\scripts\archive_pdf.cmd</Command> | |
<Arguments>"$(jobid)" "$(timestamp)" "$(username)" "$(printername)" "$(doctitle)"</Arguments> | |
</Exec> | |
</Actions> | |
</Task> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment