List Printers in PrintServers

# -------------------------------------------------------
# List Printers
# Pedro Augusto (peaugu@microsoft.com)
# Disclaimer: This script is not supported under any Microsoft standard support program or service. 
# This script is provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. 
# The entire risk arising out of the use or performance of the script and documentation remains with you. 
# In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the script be liable for any damages whatsoever 
# (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out 
# of the use of or inability to use the current script or documentation, even if Microsoft has been advised of the possibility of such damages.
#
# .\ListPrinters -PrintServers "PRNSERVER01","PRNSERVER02" -Output CSV -FileName C:\Temp\printers.csv
#
# -------------------------------------------------------

param(
 [Parameter(Mandatory=$true)]
 [string[]]$PrintServers,
 [Parameter(Mandatory=$true)]
 [ValidateSet("CSV","TXT","XML")]
 [string]$Output,
 [Parameter(Mandatory=$true)]
 [string]$FileName
)

function OutputPrinters($objs)
{
 Out-File -filePath $FileName
 if ($Output -eq "TXT")
 {
  foreach($obj in $objs)
  {
   $obj | Out-File -FilePath $FileName -Append
  }
 }

 if ($Output -eq "CSV")
 {
  $objs | Export-CSV -Path $FileName
 }

 if ($Output -eq "XML")
 {
  $objs | Export-Clixml -Path $FileName
 }
}

$printersObjs = @()

foreach($server in $PrintServers)
{
 $printers = Get-Printer -ComputerName $server
    $totalPrinters = $printers.Length
    $i = 0
 foreach($prn in $printers)
 {
        $i++
        Write-Progress -Activity "Collecting printers from $server" -Status "Collecting data from $($prn.Name)" -PercentComplete ($i / $totalPrinters * 100)

        $drv = Get-WmiObject "Win32_PrinterDriver" -Filter ("Name like '%" + $prn.DriverName + "%'") -ComputerName $server

        foreach($drvPath in $drv.DriverPath)
        {
            try
            {
                $version = Invoke-Command -ComputerName $server {param($drvPath) (Get-ItemProperty $drvPath).VersionInfo.ProductVersion} -ArgumentList $drvPath
            }catch{}

            if ($version -ne $null -and $version -ne "")
            {
                break
            }
        }

  $obj = [PSCustomObject]@{
   Type = $prn.Type
   Name = $prn.Name
   Server = $server
   Port = $prn.PortName
   Location = $prn.Location
   DriverName = $prn.DriverName
            DriverVersion = $version
  }

  $printersObjs += $obj
 }
}

OutputPrinters $printersObjs
Advertisements