0

Quick description of what I am looking for. We have customers that run our software and on the server there are anywhere up to 80 Zebra QLN420 printers all assigned Static IP Addresses. Looking for a script that will export needed printers information should we when we have to upgrade their existing services. I have found a powershell script that work wonderful that exports to csv file. The issue is that Excel isn't install on any of the customers servers. So what I am looking to do is export instead to a text file using a comma between each field. I am not a powershell coder by any means. I also found prnport.vbs on Windows that will display most of the information I need in Port Name, Hostaddress, port number but doesn't return the PrinterName.

Here is the powershell that exports to Excel.

    Param (
    string]$Printservers = "myServer"
    )

    # Create new Excel workbook
    cls
    $Excel = New-Object -ComObject Excel.Application
    #==========$Excel.Visible = $True
    $Excel = $Excel.Workbooks.Add("C:\Makeports\Something.xls")
    $Sheet = $Excel.Worksheets.Item(1)
    $Sheet.Name = "Printer Inventory"
    #======================================================
    $Sheet.Cells.Item(1,1) = "Print Server"
    $Sheet.Cells.Item(1,2) = "Printer Name"
    $Sheet.Cells.Item(1,3) = "Location"
    $Sheet.Cells.Item(1,4) = "Comment"
    $Sheet.Cells.Item(1,5) = "IP Address"
    $Sheet.Cells.Item(1,6) = "Driver Name"
    $Sheet.Cells.Item(1,7) = "Driver Version"
    $Sheet.Cells.Item(1,8) = "Driver"
    $Sheet.Cells.Item(1,9) = "Shared"
    $Sheet.Cells.Item(1,10) = "Share Name"
    #=======================================================
    $intRow = 2
    $WorkBook = $Sheet.UsedRange
    $WorkBook.Interior.ColorIndex = 40
    $WorkBook.Font.ColorIndex = 11
    $WorkBook.Font.Bold = $True
    #=======================================================

    # Get printer information
    ForEach ($Printserver in $Printservers)
    {   $Printers = Get-WmiObject Win32_Printer -ComputerName $Printserver
    ForEach ($Printer in $Printers)
    {
    if ($Printer.Name -notlike "Microsoft XPS*")
    {
        $Sheet.Cells.Item($intRow, 1) = $Printserver
        $Sheet.Cells.Item($intRow, 2) = $Printer.Name
        $Sheet.Cells.Item($intRow, 3) = $Printer.Location
        $Sheet.Cells.Item($intRow, 4) = $Printer.Comment
        
        If ($Printer.PortName -notlike "*\*")
        {   $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter "name = '$($Printer.Portname)'" -ComputerName $Printserver
            ForEach ($Port in $Ports)
            {
                $Sheet.Cells.Item($intRow, 5) = $Port.HostAddress
            }
        }
   
        ####################       
        $Drivers = Get-WmiObject Win32_PrinterDriver -Filter "__path like '%$($Printer.DriverName)%'" -ComputerName $Printserver
        ForEach ($Driver in $Drivers)
        {
            $Drive = $Driver.DriverPath.Substring(0,1)
            $Sheet.Cells.Item($intRow, 7) = (Get-ItemProperty ($Driver.DriverPath.Replace("$Drive`:","\\$PrintServer\$Drive`$"))).VersionInfo.ProductVersion
            $Sheet.Cells.Item($intRow,8) = Split-Path $Driver.DriverPath -Leaf
        }
        ####################      
        $Sheet.Cells.Item($intRow, 6) = $Printer.DriverName
        $Sheet.Cells.Item($intRow, 9) = $Printer.Shared
        $Sheet.Cells.Item($intRow, 10) = $Printer.ShareName
        $intRow ++
    }
}
$WorkBook.EntireColumn.AutoFit() | Out-Null
    }


    $intRow ++ 
    $Sheet.Cells.Item($intRow,1) = "Printer inventory completed"
    $Sheet.Cells.Item($intRow,1).Font.Bold = $True
    $Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 40
    $Sheet.Cells.Item($intRow,2).Interior.ColorIndex =

Any help to export it to a comma delimited file?

user692942
  • 16,398
  • 7
  • 76
  • 175
Tom
  • 151
  • 1
  • 12
  • No. I am quering printers on server and instead of exporting to csv (whiich is what the script I included - Using Excel object) I want to export to a csv or text file that doesn't use Excel in script as customers servers do not have Excel installed. Since no excel the scrip fails to execute when trying to use Excel object – Tom Sep 14 '20 at 15:30
  • Assumed you created it on the server and wanted to pass the CSV to clients who didn't have Excel installed. Can't see in the code where you export to CSV in the code (as you claim) but not sure how you expect to run PowerShell against Excel without Excel installed. You'd be better off just creating a CSV from scratch and ignore using Excel altogether. – user692942 Sep 14 '20 at 15:34
  • Does this answer your question? [Create/populate a csv file with Powershell](https://stackoverflow.com/a/44113932) – user692942 Sep 14 '20 at 15:35
  • Sorry. The part where s says "C:\Makeports\Something.xls" i chnaged from csv. But he part hat is not working in that code is $Excel = New-Object -ComObject Excel.Application – Tom Sep 14 '20 at 15:42
  • Think that [2nd dup target](https://stackoverflow.com/questions/63886784/how-to-export-printer-information-from-a-server-to-text-file?noredirect=1#comment112973785_63886784) should point you in the right direction. – user692942 Sep 14 '20 at 15:53
  • what do you mean by 2nd dup targer? – Tom Sep 14 '20 at 16:16
  • The second link suggesting this is a duplicate, the link points to the comment with the target question, that should help you. – user692942 Sep 14 '20 at 16:45

1 Answers1

1

Here is how I got what I was looking for.

    $Printservers = "myServer"
    $OutPutFile = (Get-Location).Path + "\allprinters8.dat"
    New-Item $OutPutFile -ItemType file
    $writer = [System.IO.Streamwriter] $OutputFile
    $Text = 'Print Server,Printer Name,Ip Address,Driver Name'
    $writer.writeline($Text)
    
    ForEach ($Printserver in $Printservers)
    {   $Printers = Get-WmiObject Win32_Printer -ComputerName $Printserver        
            ForEach ($Printer in $Printers)
            {
                    if ($Printer.Name -notlike "Microsoft XPS*")
                {
                $Text = $PrintServer + "," + $Printer.Name 
               
                If ($Printer.PortName -notlike "*\*")
                            {   $Ports = Get-WmiObject Win32_TcpIpPrinterPort -Filter                 "name = '$($Printer.Portname)'" -ComputerName $Printserver
                                    ForEach ($Port in $Ports)
                                    {
                                    $Text =$Text +"," +  $Port.HostAddress + "," +         $Printer.driverName 
                    $writer.writeline($Text)
                    }
                }   
             }
        }
    } 

    $writer.close()
Tom
  • 151
  • 1
  • 12