我需要将打印机列表导出到文件中。它必须包含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
但是它不起作用。
我知道,我可以分两步进行操作,然后复制/粘贴,但是我想一步一步地学习如何做。
仅迭代并选择所需的端口对象:
$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
}
}