Public/Printing/Debug-Printer.ps1

<#
Copyright © 2024 Integris. For internal company use only. All rights reserved.
#>


FUNCTION Debug-Printer {
    <#
    .SYNOPSIS
    Diagnoses issues with a specified printer or all printers.
 
    .DESCRIPTION
    This function diagnoses issues with a specified printer or all printers. It lists all printers and their drivers, then performs diagnostics on each printer. The results of the diagnostics are returned.
 
    .PARAMETER Name
    Specifies the name of the printer to diagnose.
 
    .PARAMETER All
    Indicates that the function should diagnose all printers.
 
    .EXAMPLE
    Debug-Printer -Name "Printer1"
    Diagnoses issues with the printer named "Printer1".
 
    .EXAMPLE
    Debug-Printer -All
    Diagnoses issues with all printers.
 
    .INPUTS
    System.String. The function accepts a string input for the printer name.
    System.Management.Automation.SwitchParameter. The function accepts a switch parameter to diagnose all printers.
 
    .OUTPUTS
    System.Object[]. Returns an array of diagnostic results for the printers.
 
    .NOTES
    Ensure you have the necessary permissions to diagnose printer issues.
    #>


    [CmdletBinding(DefaultParameterSetName = 'ByName')]
    param (
        [Parameter(Position = 0, Mandatory = $true, ParameterSetName = 'ByName', ValueFromPipeline = $true)]
        [string]$Name,

        [Parameter(ParameterSetName = 'All')]
        [switch]$All
    )

    process {
        $results = @()
        if ($PSCmdlet.ParameterSetName -eq 'All') {
            # List all printers
            Get-AllPrinters | Out-Null

            # List all printers
            Get-AllPrinterDrivers | Out-Null

            $printers = Get-Printer
            foreach ($printer in $printers) {
                Write-Host "Diagnosing printer '$($printer.Name)'..." -ForegroundColor Gray
                $results += Start-PrinterDiagnostics -PrinterName $printer.Name
                Write-Host "" -ForegroundColor Gray
            }
            RETURN $results
        } else {
            Write-Host "Diagnosing printer '$Name'..." -ForegroundColor Gray
            $results += Start-PrinterDiagnostics -PrinterName $Name
            RETURN $results
        }
    }
}
New-Alias -Name Diagnose-Printer -Value Debug-Printer