Public/Printing/Test-PrinterNetworkConnectivity.ps1

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


FUNCTION Test-PrinterNetworkConnectivity {
    <#
    .SYNOPSIS
    Tests network connectivity for a specified printer or all printers.
 
    .DESCRIPTION
    This function tests the network connectivity for a specified printer or all printers. It returns the connectivity results for each printer tested.
 
    .PARAMETER Name
    Specifies the name of the printer for which to test network connectivity.
 
    .PARAMETER All
    Indicates that the function should test network connectivity for all printers.
 
    .EXAMPLE
    Test-PrinterNetworkConnectivity -Name "Printer1"
    Tests network connectivity for the printer named "Printer1".
 
    .EXAMPLE
    Test-PrinterNetworkConnectivity -All
    Tests network connectivity for 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 test connectivity for all printers.
 
    .OUTPUTS
    System.Object[]. Returns an array of connectivity test results for the printers.
 
    .NOTES
    Ensure you have the necessary permissions to test network connectivity for printers.
    #>


    [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') {
            $printers = Get-Printer
            foreach ($printer in $printers) {
                $results += Test-PrinterConnectivity -PrinterName $printer.Name
            }
            RETURN $results
        } else {
            $results += Test-PrinterConnectivity -PrinterName $Name
            RETURN $results
        }
    }
}