【问题标题】:How to export printer information from a server to text file [duplicate]如何将打印机信息从服务器导出到文本文件 [重复]
【发布时间】:2021-01-01 07:43:26
【问题描述】:

快速描述我正在寻找的内容。 我们有运行我们软件的客户,在服务器上最多有 80 台 Zebra QLN420 打印机,它们都分配了静态 IP 地址。当我们必须升级现有服务时,寻找能够导出所需打印机信息的脚本。我发现了一个很好的 powershell 脚本,它可以导出到 csv 文件。问题是 Excel 没有安装在任何客户服务器上。 所以我要做的是在每个字段之间使用逗号导出到文本文件。无论如何,我都不是 powershell 编码器。 我还在 Windows 上找到了 prnport.vbs,它会在端口名称、主机地址、端口号中显示我需要的大部分信息,但不返回 PrinterName。

这是导出到 Excel 的 powershell。

    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 =

将其导出到逗号分隔文件有什么帮助吗?

【问题讨论】:

  • 没有。我在服务器上查询打印机,而不是导出到 csv(这是我包含的脚本 - 使用 Excel 对象)我想导出到在脚本中不使用 Excel 的 csv 或文本文件,因为客户服务器没有 Excel安装。由于没有 excel,因此尝试使用 Excel 对象时脚本无法执行
  • 假设您在服务器上创建它并希望将 CSV 传递给没有安装 Excel 的客户端。无法在代码中看到导出为 CSV 的代码(如您所声称的),但不确定您希望如何在未安装 Excel 的情况下对 Excel 运行 PowerShell。您最好从头开始创建一个 CSV 而完全忽略使用 Excel。
  • 这能回答你的问题吗? Create/populate a csv file with Powershell
  • 对不起。 s 表示“C:\Makeports\Something.xls”的部分是我从 csv 中更改的。但他的部分帽子不适用于该代码是 $Excel = New-Object -ComObject Excel.Application
  • 认为2nd dup target 应该为您指明正确的方向。

标签: excel powershell csv export-to-csv


【解决方案1】:

这就是我得到我想要的东西的方式。

    $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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2019-08-25
    • 2018-02-28
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    相关资源
    最近更新 更多