You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Get-Printer与Select-Object指定列后仍显示多余列的问题

解决PowerShell远程获取打印机时多余列的问题

问题说明

使用PowerShell远程查询打印机信息时,明明在Invoke-Command的脚本块里指定了Select-Object Name, PortName, DriverName,还加上了-ExcludeProperty Status, PSComputerName, RunspaceId,但输出结果里还是会出现PSComputerName和RunspaceId列,只有Status列被成功隐藏。原脚本如下:

# Define the path to the text file
$filePath = "C:\powershell\complist.txt"

# Check if the file exists
if (Test-Path $filePath) {
    # Read the computer names from the file
    $computerNames = Get-Content $filePath

    # Loop through each computer name
    foreach ($computer in $computerNames) {
        try {
            # Use Invoke-Command to run Get-Printer on the remote computer
            $printerInfo = Invoke-Command -ComputerName $computer -ScriptBlock {
                Get-Printer | Select-Object Name, PortName, DriverName -ExcludeProperty Status, PSComputerName, RunspaceId
            }

            if ($printerInfo) {
                # Output the printer information
                Write-Host "Printers on $computer :"
                $printerInfo | Format-Table -AutoSize
            } else {
                Write-Host "No printers found on $computer."
            }
        } catch {
            Write-Host "Error retrieving printers from '$computer': $_"
        }
    }
} else {
    Write-Host "File not found: $filePath"
}

问题原因

PSComputerName和RunspaceId这两个属性是**Invoke-Command在将远程执行结果返回本地时自动添加的**,并不是远程机器上Get-Printer输出对象自带的属性。所以在远程脚本块里用-ExcludeProperty根本无法排除它们,因为那时候这两个属性还没被加上。

解决方法

只需要在本地处理结果时,明确指定要保留的列即可过滤掉多余属性,有两种简洁的实现方式:

方式1:本地用Select-Object筛选

修改输出部分的代码,在Format-Table前先通过Select-Object指定需要的列:

if ($printerInfo) {
    Write-Host "Printers on $computer :"
    $printerInfo | Select-Object Name, PortName, DriverName | Format-Table -AutoSize
}

方式2:直接在Format-Table里指定列

更简单的写法是,直接在Format-Table中明确列出要显示的列:

if ($printerInfo) {
    Write-Host "Printers on $computer :"
    $printerInfo | Format-Table Name, PortName, DriverName -AutoSize
}

修改后的完整脚本示例:

# Define the path to the text file
$filePath = "C:\powershell\complist.txt"

# Check if the file exists
if (Test-Path $filePath) {
    # Read the computer names from the file
    $computerNames = Get-Content $filePath

    # Loop through each computer name
    foreach ($computer in $computerNames) {
        try {
            # Use Invoke-Command to run Get-Printer on the remote computer
            $printerInfo = Invoke-Command -ComputerName $computer -ScriptBlock {
                Get-Printer | Select-Object Name, PortName, DriverName -ExcludeProperty Status
            }

            if ($printerInfo) {
                # Output the printer information - 仅显示指定列
                Write-Host "Printers on $computer :"
                $printerInfo | Format-Table Name, PortName, DriverName -AutoSize
            } else {
                Write-Host "No printers found on $computer."
            }
        } catch {
            Write-Host "Error retrieving printers from '$computer': $_"
        }
    }
} else {
    Write-Host "File not found: $filePath"
}

内容的提问来源于stack exchange,提问作者Dan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 04:25:58