Partilhar via


Simple Print Queue Monitor - Powershell

its taking me a long time to get back to this... been super busy in work which is always a good thing!

here is some sample code which will allow you to create a very simple print queue monitor and log the details out to a csv file for further manipulation...

 

 

<#
.SYNOPSIS
This is a powershell script which will monitor the print queue and log the data (Owner, Date and Queue) to a Log File.

.Description

Author:John McCabe
Email: john.mccabe@microsoft.com

This Code is in no way affliated to Microsoft and Therefor by using this code Microsoft accepts no liability.

.Example

Example 1

    .\PrinterQueueMonitor.ps1

Example 2

    .\PrinterQueueMonitor.ps1 -computername printsrv01 -timer 10

#>

#Input Parameters

param ([parameter(mandatory=$true)] [string]$computername,[parameter(mandatory=$true)] [int]$timer,$inputpath)

function testexist
{
    param($path)

    $tester = test-path $path
    return $tester

}

function capturewmi
{
    param ($wmi,$logpath,$time)
    #capture the current Print Job Queue
    $capture = Get-WmiObject $wmi
    #Capture the current time / date stamp
    write-host "." -NoNewline
    $date = get-date
   
    #process all the jobs in the queue
    foreach ($capt in $capture)
    {
        #Create a new object to help us output the format correctly

        $outputobj = New-Object psobject
        #Add the values and names for exporting the information later on
        Add-Member -InputObject $outputobj -MemberType NoteProperty -Name Date -Value $date
        Add-Member -InputObject $outputobj -MemberType NoteProperty -Name Queue -Value $capt.Name
        Add-Member -InputObject $outputobj -MemberType NoteProperty -Name Owner -Value $capt.owner

        if ($append -eq $true)
        {
            #export to a csv for log creation and manipulation later on
            $outputobj |export-csv $logpath -Append
        }
        else
        {
            $outputobj |export-csv $logpath -Force
        }

    }
    #stop processing for a period of time
    sleep $time

}

write-host "Welcome To The Print Queue Monitor Script" -fore Green -back black
write-host "This Code is Posted as is and no support from Microsoft will be given for this code" -Fore green -back black

#Static Variables
$wmiclass = "Win32_PrintJob"
$logdir = $env:USERPROFILE + "\Desktop\PrintJob\"

$testdir = testexist $logdir

#Create the Directory if it does not exist
if ($testdir -eq $false)
{
    New-Item -ItemType directory -path $logdir
}

#Generate the Log File name
$logfile = $computername + "_PrintJobLog.txt"
$logs = $logdir + $logfile

#Test if the log exists, append to existing log file if it exists
$testfile = testexist $logs

if ($testfile -eq $true)
{
    $append = $true
}
else
{
    $append = $false
}

#create reference for infinite loop
$nolimit = 0

#begin infinite loop

write-host "Press CTRL-C at any point to stop Data Capture" -fore yellow -back black
write-host "Check $logs to review print queue jobs and owners" -fore yellow -back black
write-host "Beginning Capture...." -fore green -back black

while ($nolimit -eq 0)
{
    capturewmi $wmiclass $logs $timer
}

Comments

  • Anonymous
    October 02, 2012
    i found the code is replacing the log file instead of appending. :)