Skip to content

Instantly share code, notes, and snippets.

@automationhaus
Last active December 9, 2016 15:50
Show Gist options
  • Save automationhaus/97ff070ba3d60b600d8450a25c0e96a7 to your computer and use it in GitHub Desktop.
Save automationhaus/97ff070ba3d60b600d8450a25c0e96a7 to your computer and use it in GitHub Desktop.
Uses WMI to pull locally installed printer information for output to CSV or other destination
function Get-InstalledPrinters
{
<#
.SYNOPSIS
Simple function to grab details regarding locally installed printers on servers
.DESCRIPTION
Uses WMI to pull locally installed printer information for output to CSV or other destination
.EXAMPLE
PS>Get-InstalledPrinters "SERVER1","SERVER2","SERVER3" -Unique
.PARAMETER Computers
String array of computer names or IP addresses
.PARAMETER Unique
Switch to enable finding of printers with unique IP addresses or "HostNames" as denoted by the WMI output
.NOTES
Britt Thompson
[email protected]
.LINK
https://gist.github.com/amesritter/97ff070ba3d60b600d8450a25c0e96a7
#>
[cmdletbinding()]
param (
[Parameter(Mandatory=$true)]
[string[]]$Computers,
[switch]$Unique
)
# Establish the array to collect all the printer objects
$Result = @()
# Loop through all given computers
foreach($Computer in $Computers)
{
# Collect all the printers for the current computer
try
{
$Printers = Get-WmiObject Win32_Printer -ComputerName $Computer
} catch { Write-Host $_.Exception.Message -ForegroundColor Red }
# Loop through the printers on this computer to collect the data
foreach($Printer in $Printers)
{
# Establish the object to add the printer data to and collect the printer port info
$Obj = New-Object PSObject
try
{
$Port = Get-WmiObject Win32_TcpIpPrinterPort | ?{ $_.Name -eq $Printer.Portname }
} catch { Write-Host $_.Exception.Message -ForegroundColor Red }
# If unique is enabled then filter out the printers that already exist in the results array
if($Unique){ $HostAddresses = $Result | %{$_.HostAddress} }
if($Port -and $HostAddresses -notcontains $Port.HostAddress)
{
# Add the properties to obj
$Obj | Add-Member -MemberType NoteProperty -Name Computer -Value $Printer.SystemName
$Obj | Add-Member -MemberType NoteProperty -Name Name -Value $Printer.Name
$Obj | Add-Member -MemberType NoteProperty -Name DriverName -Value $Printer.DriverName
$Obj | Add-Member -MemberType NoteProperty -Name PortName -Value $Printer.PortName
$Obj | Add-Member -MemberType NoteProperty -Name HostAddress -Value $Port.HostAddress
$Obj | Add-Member -MemberType NoteProperty -Name PortType -Value $Port.Description
$Obj | Add-Member -MemberType NoteProperty -Name PortNumber -Value $Port.PortNumber
$Obj | Add-Member -MemberType NoteProperty -Name Queue -Value $Port.Queue
$Obj | Add-Member -MemberType NoteProperty -Name SNMPCommunity -Value $Port.SNMPCommunity
$Obj | Add-Member -MemberType NoteProperty -Name SNMPEnabled -Value $Port.SNMPEnabled
# Update the result array with this object
$Result += $Obj
}
}
}
# Output the results
$Result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment