This post is about a small script to connect a Windows Print Server remotely and list all Printers attached to the Server.It will also provide printer IP Address, printer status etc.
I will use Microsoft Windows Operating System WMI provider in Powershell to connect and generate details from the print server. Through this script, I will collect Printer Name, IP address, Type , status for each printer on the server with exception of “XPS” printers. You may add PDF, One Note etc. in exclusion list based on your requirements.
Make sure you have connectivity to the remote server and authorization to run script remotely. I will use print server name and the output file path as input to the script.Feel free to update script to set those as parameters.
Here is the complete script!
$printServerName = "<myprintserver.fqdn>"
$oFile = "<Some Path>\Printers_Detail.csv"
if(Test-Path $oFile){
Remove-Item -Path $oFile -Force
}
"Server Name,Printer Name,Type,IP Address,Staus" | Out-File $oFile -append -Encoding ASCII
$printers = Get-WmiObject -class win32_printer -ComputerName $printServerName
ForEach($printer in $printers)
{
$IPAddress = $printerName = $printerType = $printerStatus = ""
if($null -ne $printer -Or $printer.Name -notlike "*XPS*") # I am excluding XPS Printers. Feel free to add your exclusions here
{
if($null -ne $printer.Attributes){
if($printer.Attributes -eq 64){ # Based on my experience, Attribute 64 is for Local printers. You may have different value. Verify at your end and update accordingly.
$printerType = "Local Printer"
}
else{
$printerType = "Network/Session Printer"
}
$printerName = $printer.Name
$ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($printer.Portname)'" -ComputerName $printServerName
ForEach ($port in $ports){
if($port.Name -eq $printer.Portname){
$IPAddress = $port.HostAddress
}
}
if (test-Connection -ComputerName $IPAddress -Count 2 -Quiet){
$printerStatus = "ONLINE"
}
else {
$printerStatus = "OFFLINE"
}
}
"$printServerName,$PrinterName,$printerType,$IPAddress,$printerStatus" | Out-File $oFile -append -Encoding ASCII
}
}