Public/Printing/Test-PrinterConnectivity.ps1

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


FUNCTION Test-PrinterConnectivity {
    <#
    .SYNOPSIS
    Tests network connectivity for a specified printer.
 
    .DESCRIPTION
    This function tests the network connectivity for a specified printer. It performs various network diagnostic tests and returns the results. If the printer or its IP address cannot be found, it displays an appropriate message and returns the error details.
 
    .PARAMETER PrinterName
    Specifies the name of the printer for which to test network connectivity.
 
    .EXAMPLE
    Test-PrinterConnectivity -PrinterName "Printer1"
    Tests network connectivity for the printer named "Printer1".
 
    .INPUTS
    System.String. The function accepts a string input for the printer name.
 
    .OUTPUTS
    System.Object[]. Returns an array of connectivity test results for the printer.
 
    .NOTES
    Ensure you have the necessary permissions to test network connectivity for printers.
    #>


    param (
        [string]$PrinterName
    )

    $results = @()
    $printer = Get-Printer -Name $PrinterName
    if ($printer) {
        $printerPort = Get-PrinterPort -Name $printer.PortName

        if ($printerPort.Name -like "WSD-*") {
            Write-Host "Network tests cannot be conducted on WSD printers ('$($printer.Name)')."
            return
        }
        elseif ($printerPort.Name -like "nul:") {
            Write-Host "Network tests cannot be conducted on OneNote printers ('$($printer.Name)')."
            return
        }
        elseif ($printerPort.Name -like "PORTPROMPT:") {
            Write-Host "Network tests cannot be conducted on PDF printers ('$($printer.Name)')."
            return
        }

        $ipAddress = $printerPort.PrinterHostAddress

        if ($ipAddress) {
            Write-Host "Testing network connectivity for printer '$($printer.Name)' with IP address '$ipAddress'..." -ForegroundColor Yellow

            # Additional network diagnostic tests
            $pingTest = Test-PrinterPing $ipAddress
            $portTest = Test-PrinterPort $ipAddress
            $DNSTest = Test-PrinterDNS $ipAddress
            $tracertTest = Test-PrinterTraceroute $ipAddress

            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                PrinterName = $PrinterName
                ipAddress = $ipAddress
                pingTest = $pingTest
                portTest = $portTest
                DNSTest = $DNSTest
                tracertTest = $tracertTest
                error = $null
            }
            RETURN $Results
        } else {
            Write-Host "Failed to retrieve IP address for printer '$PrinterName'." -ForegroundColor Red
            $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
                PrinterName = $PrinterName
                ipAddress = $ipAddress
                pingTest = $null
                portTest = $null
                DNSTest = $null
                tracertTest = $null
                error = "ERROR: Failed to retrieve IP address"
            }
            RETURN $Results
        }
    } else {
        Write-Host "Printer '$PrinterName' not found." -ForegroundColor Red
        $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{
            PrinterName = $PrinterName
            ipAddress = $null
            pingTest = $null
            portTest = $null
            DNSTest = $null
            tracertTest = $null
            error = "ERROR: Printer not found."
        }
        RETURN $Results
    }
}