Public/Add-SpecPrinter.ps1
function Add-SpecPrinter { <# .SYNOPSIS Adds a specified printer to the system using the provided parameters. .DESCRIPTION The Add-SpecPrinter function adds a printer to the system by performing the following steps: 1. Attempts to install the specified printer driver. 2. Verifies if the printer driver exists. If it exists, installs the printer. 3. Returns an array containing error codes if any errors occurred during the process or a success code. .PARAMETER printerName Specifies the name of the printer to be added. This parameter is mandatory. .PARAMETER printerPort Specifies the name of the printer port. This parameter is mandatory. .PARAMETER printDriverName Specifies the name of the printer driver. This parameter is mandatory. .OUTPUTS Returns an array of codes indicating the status of the operation. The following codes can be returned: 500 - Printer successfully added. 501 - The specified printer already exists. 502 - Unable to add the printer due to an unknown error. .EXAMPLE Add-SpecPrinter -printerName "Printer1" -printerPort "LPT1" -printDriverName "HP Universal Driver" This example adds a printer named "Printer1" using the printer port "LPT1" and the driver "HP Universal Driver". .NOTES Author: owen.heaume Version: 1.0 #> [cmdletbinding()] param ( [Parameter(Mandatory = $true)] $printerName, [Parameter(Mandatory = $true)] $printerPort, [Parameter(Mandatory = $true)] $printDriverName ) Begin { } Process { try { Write-Verbose "Attempting to install printer $printerName" Add-Printer -Name $printerName -PortName $printerPort -DriverName $printDriverName -ErrorAction Stop -ErrorVariable x Write-Verbose "Printer $printerName installed on port $printerPort using driver $printDriverName" return 500 } catch { write-warning "Printer failed to install" if ($x -match "The specified printer already exists.") { Write-Warning "The specified printer already exists." return 501 } else { Write-Warning "The error message was: $x" return 502 } } } } |