'Get-PrintServerPrinterIPs-Remote.ps1'
#Set PS7 window name
$Host.UI.RawUI.WindowTitle = $ENV:Computername
#Restrict Script to be run ONLY in PS7:
#Automatic check
#Define required version
$Required Version = [Version]"7.0"
#Requires -Version 7.0
#Restrict Script to be run ONLY in PS7:
# Compare with the current PS version
if ($PSVersionTable.PSVersion -lt $RequiredVersion) {
Write-Error "This script requires PowerShell version $RequiredVersion or newer. You are running $($PSVersionTable.PSVersion)."
exit 1
}
# Define required version
$RequiredVersion = [Version]"7.0"
#Requires -Version 7.0
#Restrict Script to be run ONLY in PS7:
#Import Excel
#NOTE Remark the next line once install is confirmed
Install-Module -Name ImportExcel -verb -Scope CurrentUser -AllowClobber:$false -SkipPublisherCheck -AcceptLicense
Import-Module -Name ImportExcel -Verbose -Scope Local
#Set day
$day = get-date -Format yyyyMMdd
# Specify the print server names
$printServers = read-host -Prompt "What print server?"
# UnRemark the line below to run locally
# $printServers = $env:computername
#Clean up files
rm -Force "c:\powershell\reports\*AllPrinterInfo.xlsx" -ea SilentlyContinue|Out-Null
# Create an array to hold printer information
$printerInfo = @()
foreach ($printServer in $printServers) {
# Get all printers from the specified print server
$printers = Get-Printer -ComputerName $printServer -Full -ErrorAction SilentlyContinue
foreach ($printer in $printers) {
# Get the printer's port information
$port = Get-PrinterPort -Name $printer.PortName -ComputerName $printServer -ErrorAction SilentlyContinue
# Now attempt to resolve the printer's IP address from DNS
$dnsIpAddress = $null
try {
$dnsResult = Resolve-DnsName -Name $printer.Name -ErrorAction Stop
$dnsIpAddress = $dnsResult | Where-Object { $_.QueryType -eq 'A' } | Select-Object -First 1 -ExpandProperty IPAddress
} catch {
$dnsIpAddress = "N/A" # If DNS resolution fails, set to N/A
}
# Get the Queue Status from the printer
$queueStatus = $printer.PrinterStatus
# Create a custom object to store printer details
$printerDetails = [PSCustomObject]@{
PrintServer = $printServer
Name = $printer.Name
DriverName = $printer.DriverName
PortName = $printer.PortName
Shared = $printer.Shared
Published = $printer.Published
DeviceType = $printer.DeviceType
IPv4Address = if ($port.PrinterHostAddress) { $port.PrinterHostAddress } else { "N/A" }
DnsIPAddress = if ($dnsIpAddress) { $dnsIpAddress } else { "N/A" }
QueueStatus = $queueStatus
}
# Add the printer details to the array
$printerInfo += $printerDetails
}
}
# Deduplicate the printer information based on Name and PrintServer
$deduplicatedPrinterInfo = $printerInfo | Select-Object -Property PrintServer, Name, DriverName, PortName, Shared, Published, DeviceType, IPv4Address, DnsIPAddress, QueueStatus -Unique
# Sort the printer information by PrintServer
$sortedPrinterInfo = $deduplicatedPrinterInfo | Sort-Object PrintServer
# Output the printer information to an Excel file
# NOTE: Always make sure your paths exist
$excelFilePath = "c:\powershell\Reports\$day-$printServer-AllPrinterInfo.xlsx"
$sortedPrinterInfo | Export-Excel -Path $excelFilePath -AutoSize -WorksheetName "PrinterInfo"
$sortedPrinterInfo
# Inform the user
Write-Host "Printer information exported to $excelFilePath"
[Here you could add a line to call excel, like “‘start excel $excelFilePath”]


Comments
One response to “Get Print Server Printer IPs Remotely”
Great resource for AI tools! As someone focused on risk and efficiency, tyy.AI streamlines the search for innovative solutions. Check out their AI Business Solutions for smart, curated picks.