Thought I’d share this for the community, It made our deployment of TCPIP printers that are not on a print server simple! Hope it helps someone. Open to suggestions on what to change / fix!
For our Evaluation:
exit 0
We Initailly deploayed with exit 0 since we we planned on running the worklet on demand, so the evaluation was going to be skipped anyways, but I’d imagine changing the Evaluation to maybe look for a specified printer name if not found then send to remediation
Remediation:
# Upload the driver files in a ZIP folder to the worklet
# In this example we used HP Printer Drivers
# Here we Install the Printer Driver, If your computers already have the driver installed, you can comment this out
$scriptBlock = {
$Folder = "HPPrintDriver"
New-Item -ItemType Directory -Path $Folder
Expand-Archive -Path "HPDriver.zip" -DestinationPath "HPPrintDriver"
if(Test-Path $Folder){
pnputil /add-driver "HPPrintDriver\HPDriver\*.inf" /install
}
}
$exitCode = & "$env:SystemRoot\sysnative\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -WindowStyle Hidden -NoProfile -NonInteractive -Command $scriptBlock
# To make it simple to verify the driver was installed on the computer I just print:
$exitcode
# Below is where the printer gets mapped to the computer
# Add the Printer Driver
Add-PrinterDriver -Name "HP OfficeJet Pro 7740 series"
# We also add the Printer port
# "X.X.X.X" -EQ your printer IP
add-printerport -name "NAME YOUR PRINTER PORT" -printerhostaddress "X.X.X.X"
# Now we run the install printer command
Add-printer -Name "MY PRINTER" -DriverName "HP OfficeJet Pro 7740 series" -PortName "PrintPort"
# The printer will now show up under devices and printers on your computer
I’ve deployed printers with this worklet to about 20 computers, users are able to print with no issues
This also standardizes the printer named on each device you deploy this to, making uninstallation super easy!
If you need to uninstal lthe printer you can use this worklet!
For Evaluation we used exit 0 since we ran this worklet on demand
For the removal of the printer using Remediation :
# Uninstalles Printer installed Via Worklet
# Edit the Below Variable to match your target printer
############################
$PrinterIP = "X.X.X.X"
############################
##### Begin Uninstall Function ##########################
function Remove-LocalPrinter {
param (
rstring]$PrinterName
)
# Check if the printer exists
$printer = Get-WmiObject -Query "SELECT * FROM Win32_Printer WHERE Name = '$PrinterName'"
if ($printer -eq $null) {
Write-Host "Printer '$PrinterName' not found."
return
}
# Delete the printer
$printer.Delete()
# Remove the printer port
$portName = $printer.PortName
$port = Get-WmiObject -Query "SELECT * FROM Win32_TCPIPPrinterPort WHERE Name = '$portName'"
if ($port -ne $null) {
$port.Delete()
}
# Remove the printer driver
$driverName = $printer.DriverName
$driver = Get-WmiObject -Query "SELECT * FROM Win32_PrinterDriver WHERE Name = '$driverName'"
if ($driver -ne $null) {
$driver.Delete()
}
Write-Host "Printer '$PrinterName' has been removed along with its port and driver."
exit 0
}
################ End Uninstall Function ######################################
$PortIP = get-wmiobject win32_tcpipprinterport | Where-Object {$_.HostAddress -like "$PrinterIP"} | Select-Object -ExpandProperty "HostAddress"
if($PortIP -eq "$PrinterIP"){
# Find the PortName
$PortName = get-wmiobject win32_tcpipprinterport | Where-Object {$_.HostAddress -like "$PrinterIP"} | Select-Object -ExpandProperty "Name"
# Find the Printer Name
$PrinterName = Get-Printer | Where-Object {$_.PortName -like "$PortName"} | Select-Object -ExpandProperty "Name"
Remove-LocalPrinter -PrinterName $PrinterName
}else {
Write-Host "Printer Not Found"
exit 0
}
Again, we used this to add / remove TCPIP printers form computers that were not on a print server.
Hope it Helps!