【问题标题】:Using Format-Table with pscustomobject将格式表与 pscustomobject 一起使用
【发布时间】:2015-05-20 23:27:45
【问题描述】:

我想将 Format-Table -Autosize 与 pscustomobject 一起使用。

我想要相当于:

Get-Process | ft Id,ProcessName -AutoSize

我试过了(虽然输出位于中心)

Get-Process | %{
            [pscustomobject]@{
                ID   = $_.Id
                ProcessName  = $_.ProcessName
            }
}

它可以工作,但是当我使用 Format-Table -Autosize 它不起作用时,它会添加带有新行的新标题。

Get-Process | %{
            [pscustomobject]@{
                ID   = $_.Id
                ProcessName  = $_.ProcessName
            } | Format-Table -AutoSize
}

如何解决这个问题?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您的管道位置错误。

    Get-Process | %{
                [pscustomobject]@{
                    ID   = $_.Id
                    ProcessName  = $_.ProcessName
                } 
    } | Format-Table -AutoSize
    

    您告诉它为每个元素输出一个表,而不是按预期使用管道。

    【讨论】:

    • 有时解决方案就在我们的眼皮底下,而我们看不到。
    【解决方案2】:

    您在错误的位置向Format-Table 发送消息:

    Get-Process | % {
        [pscustomobject]@{
            ID   = $_.Id
            ProcessName  = $_.ProcessName
        } 
    } | Format-Table -AutoSize
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-03
      • 2019-09-26
      • 2011-11-25
      • 2015-12-30
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      相关资源
      最近更新 更多