Custom Printer Deployment

Copper Contributor

Hi everyone, great to be here.

 

I will try to explain this in the best way I can, in hope that someone can help me. I have been tasked to deploy Lexmark Print & Hold to 240 endpoints. So far I have written a Powershell script to complete the following:

 

  1. Remove existing printers and drivers
  2. Uninstall the Lexmark printer driver package (otherwise the custom package installation fails)
  3. Install a custom Lexmark driver package built using their package builder
  4. Add a 'dummy' printer as the software needs a physical device to install.
  5. Add 5 new printer ports for the additional printers
  6. Add 5 printers to the specified ports

At this stage, there is only one driver installed, and that is the custom driver package created using the official software. The first printer whos port is added as part of the package installation, works as expected and the Print & Hold feature is enforced. The 5 additional printers added by my Powershell script do not have the custom settings applied, even though they are using the same driver.

 

Is this something I have done wrong? Can Powershell do it a better way? I am new to Powershell.

 

Many thanks in advance,
Bret

 

 

########################################
## Custom Lexmark Driver Installation ##
## Bret Whitcombe                     ##
########################################

$logfilePath = "C:\RB"
$InstallPath = "C:\Lexmark"
$driverName = "Lexmark Universal v2 PostScript 3 Emulation"
$printerPortName1 = "Central 1"
$printerPortName2 = "Central 2"
$printerPortName3 = "Group 1"
$printerPortName4 = "Group 2"
$printerPortName5 = "Payroll"
$printerPort1 = "xxx.xxx.xxx.xxx"
$printerPort2 = "xxx.xxx.xxx.xxx"
$printerPort3 = "xxx.xxx.xxx.xxx"
$printerPort4 = "xxx.xxx.xxx.xxx"
$printerPort5 = "xxx.xxx.xxx.xxx"
$printerName1 = "Central 1"
$printerName2 = "Central 2"
$printerName3 = "Group 1"
$printerName4 = "Group 2"
$printerName5 = "Payroll"

# Create directory for logging
if (Test-Path $logfilePath) {
    Write-Host "Directory already exists"
} else {
    Write-Host "Creating logfile directory"
    New-Item "C:\RB" -ItemType Directory
}

# Enable logging
Start-Transcript -Append C:\RB\LexmarkInstallLog.txt

# Remove-PrinterDriver -Name "All Lexmark Printers"
Remove-Printer -Name "*Lexmark*"
Start-Sleep -Seconds 5
Get-PrinterDriver -Name *Lexmark* | Remove-PrinterDriver -Verbose -ErrorAction SilentlyContinue

# Uninstall Lexmark driver package
Get-CimInstance -Class Win32_Product | Where-Object Name -like "*Lexmark*" | fl
Start-Process msiexec.exe -Argument "/x {BF52F3BB-5614-A294-7687-18032798CBD2} /qn"

# Delete Lexmark folder if exists
$InstallPath = "C:\Lexmark\"
if (Test-Path $InstallPath ) {
    Write-Host "This folder exists"
    Remove-Item $InstallPath -Force
} else {
    Write-Host "This folder doesn't exist"
}

# Download custom driver package *** downloads
New-Item "C:\Lexmark" -ErrorAction Ignore -Type Directory
Invoke-WebRequest -Uri "https://dl.***.co.uk/***/Lexmark/CustomPackage/InstallationPackage.zip" -OutFile "C:\Lexmark\InstallationPackage.zip"
Invoke-WebRequest -Uri "https://dl.***.co.uk/***/Lexmark/CustomPackage/LexmarkPkgInstaller.exe" -OutFile "C:\Lexmark\LexmarkPkgInstaller.exe"
Invoke-WebRequest -Uri "https://dl.***.co.uk/***/Lexmark/CustomPackage/PackageSummary.html" -OutFile "C:\Lexmark\PackageSummary.html"

# Install custom driver package
C:\Lexmark\LexmarkPkgInstaller.exe /ip=10.10.10.10 /p=DummyPort

# Wait for driver installation to complete
Start-Sleep -Seconds 300

#############################################
#         Add printers and ports            #
#  Check if ports exist and install if not. # 
#############################################

# Central 1 Printer Installation
    try{
	  #try to get printer port. If that returns a value port is already installed
	    Get-PrinterPort -name $printerPortName1
	    Write-Warning "Printer port with name $($printerPortName1) already exists"
}catch{
	  #if Get-PrinterPort returns an error there's no installed port so we can install
	    Add-PrinterPort -Name $printerPortName1 -PrinterHostAddress $printerPort1
}

    try {
      # Add the printer
      Add-Printer -Name $printerName1 -DriverName $driverName -PortName $printerPortName1 -ErrorAction stop
    } catch {
      Write-Host $_.Exception.Message -ForegroundColor Red
      break
    }

    Write-Host "Central 1 - Printer successfully installed" -ForegroundColor Green
# End Central 1 Printer Installation

# Central 2 Printer Installation
    try{
	  #try to get printer port. If that returns a value port is already installed
	    Get-PrinterPort -name $printerPortName2
	    Write-Warning "Printer port with name $($printerPortName2) already exists"
}catch{
	  #if Get-PrinterPort returns an error there's no installed port so we can install
	    Add-PrinterPort -Name $printerPortName2 -PrinterHostAddress $printerPort2
}

    try {
      # Add the printer
      Add-Printer -Name $printerName2 -DriverName $driverName -PortName $printerPortName2 -ErrorAction stop
    } catch {
      Write-Host $_.Exception.Message -ForegroundColor Red
      break
    }

    Write-Host "Central 2 - Printer successfully installed" -ForegroundColor Green
# End Central 2 Printer Installation

# Group 1 Printer Installation
    try{
	  #try to get printer port. If that returns a value port is already installed
	    Get-PrinterPort -name $printerPortName3
	    Write-Warning "Printer port with name $($printerPortName3) already exists"
}catch{
	  #if Get-PrinterPort returns an error there's no installed port so we can install
	    Add-PrinterPort -Name $printerPortName3 -PrinterHostAddress $printerPort3
}

    try {
      # Add the printer
      Add-Printer -Name $printerName3 -DriverName $driverName -PortName $printerPortName3 -ErrorAction stop
    } catch {
      Write-Host $_.Exception.Message -ForegroundColor Red
      break
    }

    Write-Host "Group 1 - Printer successfully installed" -ForegroundColor Green
# End Group 1 Printer Installation

# Group 2 Printer Installation
    try{
	  #try to get printer port. If that returns a value port is already installed
	    Get-PrinterPort -name $printerPortName4
	    Write-Warning "Printer port with name $($printerPortName4) already exists"
}catch{
	  #if Get-PrinterPort returns an error there's no installed port so we can install
	    Add-PrinterPort -Name $printerPortName4 -PrinterHostAddress $printerPort4
}

    try {
      # Add the printer
      Add-Printer -Name $printerName4 -DriverName $driverName -PortName $printerPortName4 -ErrorAction stop
    } catch {
      Write-Host $_.Exception.Message -ForegroundColor Red
      break
    }

    Write-Host "Group 2 - Printer successfully installed" -ForegroundColor Green
# End Group 2 Printer Installation

# Payroll Printer Installation
    try{
	  #try to get printer port. If that returns a value port is already installed
	    Get-PrinterPort -name $printerPortName5
	    Write-Warning "Printer port with name $($printerPortName5) already exists"
}catch{
	  #if Get-PrinterPort returns an error there's no installed port so we can install
	    Add-PrinterPort -Name $printerPortName5 -PrinterHostAddress $printerPort5
}

    try {
      # Add the printer
      Add-Printer -Name $printerName5 -DriverName $driverName -PortName $printerPortName5 -ErrorAction stop
    } catch {
      Write-Host $_.Exception.Message -ForegroundColor Red
      break
    }

    Write-Host "Payroll - Printer successfully installed" -ForegroundColor Green
# End Payroll Printer Installation

	Write-Host "Printer installation script complete" -ForegroundColor Green

# Disable logging
Stop-Transcript

 

1 Reply

@Bret_Whitcombe 

 

Hi, Bret.

 

I can't test this script out but one observation is that your various calls to Get-PrinterPort (beginning line 74) do not include "-ErrorAction:Stop" meaning an exception will not be thrown even if the port is not found - assuming your system has $ErrorActionPreference as Continue (the default value).

 

This means your subsequent Add-PrinterPort would not be being called.

 

Here's a simple one-liner to demonstrate the incorrect flagging of "success":

Example

try { Get-PrinterPort -Name "LPT2"; "Success"; } catch { "Failure" }

 

Output

LainRobertson_0-1701818784222.png

 

 

And here's the expected one-liner and behaviour:

Example

try { Get-PrinterPort -Name "LPT2" -ErrorAction:Stop; "Success"; } catch { "Failure" }

Output

LainRobertson_1-1701818880256.png

 

Cheers,

Lain