PSitSupport.psm1

#Generated at 08/05/2019 14:28:11 by LIENHARD Laurent
function Get-PrintJobLog {
    <#
    .SYNOPSIS
        Retrieve print log
    .DESCRIPTION
        Long description
    .EXAMPLE
        PS C:\> <example usage>
        Explanation of what the example does
    .INPUTS
        Inputs (if any)
    .OUTPUTS
        Output (if any)
    .NOTES
        General notes
    #>

    [CmdletBinding(DefaultParameterSetName = "ByDay")]
    param (
        [Parameter(ParameterSetName = "ByDay")]
        [Parameter(ParameterSetName = "ByInterval")]
        [System.String]$ComputerName,
        [Parameter(ParameterSetName = "ByDay")]
        [System.DateTime]$Date = (Get-Date),
        [Parameter(ParameterSetName = "ByInterval")]
        [System.DateTime]$StartDate,
        [Parameter(ParameterSetName = "ByInterval")]
        [System.DateTime]$EndDate,
        [Parameter(ParameterSetName = "ByDay")]
        [Parameter(ParameterSetName = "ByInterval")]
        [pscredential]$Credential
    )

    begin {
        $ScriptName = (Get-Variable -name MyInvocation -Scope 0 -ValueOnly).Mycommand
        Import-Module PSDateManagement
    }

    process {
        write-verbose "[$Scriptname] - BEGIN PROCESS"

        #region <Set Date>
        switch ($PSCmdlet.ParameterSetName) {
            "ByDay" {
                Write-Verbose "[$Scriptname] - $Date"
                $DateUse = New-DMDate -Date $Date
            }
            "ByInterval" {
                Write-Verbose "[$Scriptname] - Startdate : $($StartDate) and EndDate : $($EndDate)"
                $DateUse = New-DMDate -StartDate $StartDate -EndDate $EndDate
            }
        }
        #endregion

        #region <Creation du fichier csv>

        $csvfile = $ScriptPath + "Printing Audit - " + $ComputerName + " - " + (Get-Date).ToString("yyyy-MM") + ".csv"
        Write-Verbose "[$Scriptname] - Fichier csv : $csvfile"
        if ((Test-Path -Path $ScriptPath+$csvfile) -eq $true) {
            remove-item $csvfile
        }
        write-output "Server; Date; Full Name; Client; Printer Name; Print Size; Pages" | Out-File $csvfile
        #endregion

        $GetWinEventParams = @{
            ComputerName = $ComputerName
            ErrorAction  = "SilentlyContinue"
        }

        if ($PSBoundParameters['verbose']) {
            $GetWinEventParams.add('verbose', $verbose)
        }
        if ($PSBoundParameters['Credential']) {
            $GetWinEventParams.add('credential', $Credential)
        } else {
            $Credential = Get-Credential -Message "Enter Admin Print account"
            $GetWinEventParams.add('credential', $Credential)
        }

        Write-Verbose "[$Scriptname] - Find print log on $($ComputerName) between $($DateUse.FirstDay) and $($DateUse.LastDay)"
        $EventLogs = Get-WinEvent @GetWinEventParams -FilterHashTable @{ProviderName = "Microsoft-Windows-PrintService"; StartTime = $DateUse.FirstDay; EndTime = $DateUse.LastDay; ID = 307 }

        ForEach ($LogEntry in $EventLogs) {
            #Get print job details
            $time = $LogEntry.TimeCreated
            $entry = [xml]$LogEntry.ToXml()
            $Username = $entry.Event.UserData.DocumentPrinted.Param3
            $Computer = $entry.Event.UserData.DocumentPrinted.Param4
            $PrinterName = $entry.Event.UserData.DocumentPrinted.Param5
            $PrintSize = $entry.Event.UserData.DocumentPrinted.Param7
            $PrintPages = $entry.Event.UserData.DocumentPrinted.Param8

            #$Write Log to CSV file
            $strOutput = $PrintServer + "; " + $time.ToString() + "; " + $Username + "; " + $Computer + "; " + $PrinterName + "; " + $PrintSize + "; " + $PrintPages
            write-output $strOutput | Out-File $csvfile -append
        }

        #endregion
    }

    end {
    }
}