如何获得两个cmdlet(get-printer和get-printerport)的结果的唯一输出?

问题描述 投票:0回答:1

我需要将打印机列表导出到文件中。它必须包含Get-Printer中的Name,DriverName等值,以及cmdlet Get-PrinterPort中的PrinterHostAddress值。我认为两个cmdlet的值'Name'都可以用作创建联合的键。我已经尝试过像:

$a = get-printer
$b = get-printerport
Compare-Object $a $b -PassThru -IncludeEqual | Select Name, Comment, Location, DriverName, PrinterHostAddres

但是它不起作用。

我知道,我可以分两步进行操作,然后复制/粘贴,但是我想一步一步地学习如何做。

powershell printers
1个回答
0
投票

仅迭代并选择所需的端口对象:

$Printers = Get-Printer

$Printers | ForEach-Object {
    #Get the printer port
    $port = Get-PrinterPort | Where-Object -Property Name -like $_.PortName

    [pscustomobject]@{
        PrinterName = $_.Name
        Comment = $_.Comment
        Location = $_.Location
        Driver = $_.DriverName
        PortName = $port.Name
        IPAddress = $port.PrinterHostAddress
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.