PowerShell – Locate Installed Printers On Windows Servers

Recently, there was a need for us to gather a listing of the printers that we have deployed on our File and Print Server servers. Being a fairly distributed environment, a lot of time could have been spent logging into each server and manually gathering the data. However, this was a great time to get my feet wet and have some PowerShell/WMI fun.

Out of correctness, I would like to note that this script is Frankenstein-ish script from sources all over the Internet. Since I am not trying to do anything all that difficult, piecing together the framework script and filling in the blanks where needed is an awesome way to go. Thanks to the people online for sharing your knowledge.

Without further ado:

### # Search through AD for listing of workstations in HKG ### $strWorkstationList = @() # Connect to the Domain at the LDAP location specified $ou = [ADSI]"LDAP://ou=FPS,OU=Servers,DC=domain,dc=local" # Parse through the objects in the OU location for devices that are described as "computer" ForEach ($child in $ou.psbase.Children) { if ($child.ObjectCategory -like '*computer*') { #Write results to the screen for diagnostic purposes #Write-Host $child.Name #Append the results to the strWorkstation string $strServerList += $child.Name }#end if }#end for each loop ### # Parse through the list of FPS Servers from the OU and look for Printer information from them (they need to be online) ### Write-Host "Beginning to parse through the FPS Server list and locate Printer information" Write-Host $strServerList # Ping the host to ensure that it is available on the network. # NOTE: If the firewall is configured to disallow ICMP, it will appear to be down and not be checked. # NOTE to the NOTE: if the firewall is configured to disallow ICMP, it is likely WMI is disallowed as well. ForEach ($serverName in $strServerList) { try { $ping = new-object System.Net.NetworkInformation.Ping $Reply = $ping.Send($serverName) #Write-Host "Ping to " $serverName " is " $Reply.status if($Reply.status -ne "Success") { Write-Host $serverName " not responding on the network -- Status: " $Reply.status } else { Write-Host $serverName " is responding on the network -- Status: " $Reply.status $Reply.RoundtripTime "ms" Write-Host "Connecting to " $serverName # Connect via WMI to the workstation ('aka - $serverName) $printerConfig = Get-WmiObject -Class win32_printer -ComputerName $serverName -ErrorVariable $exceptionVariable $myCol = @() # Parse through each NIC on the workstation (since they are configured with multiple) ForEach ($printer in $printerConfig) { # Define the object holding the output per NIC $myObj = "" | Select-Object Description, DeviceID, DriverName, InstallDate, Local, Network, PortName, ServerName, ShareName # Collect values for the NIC $myObj.Description = $printer.Description $myObj.DeviceID = $printer.DeviceID $myObj.DriverName = $printer.DriverName $myObj.InstallDate = $printer.InstallDate $myObj.Local = $printer.Local $myObj.Network = $printer.Network $myObj.PortName = $printer.PortName $myObj.ServerName = $printer.ServerName $myObj.ShareName = $printer.ShareName $myCol += $myObj } $myCol }# End if(Server successfully pings test) } # End try catch [SystemException] { $var += 1 }# End Catch [DotNetMethodException} } #End ForEach ($serverName in $strServer) Write-Host "Completed parsing through the Server list and locating Printer information"


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s