Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 格式表-按单行显示数组属性分组_Powershell_Grouping_Formattable - Fatal编程技术网

Powershell 格式表-按单行显示数组属性分组

Powershell 格式表-按单行显示数组属性分组,powershell,grouping,formattable,Powershell,Grouping,Formattable,我正在创建一个具有两个属性的自定义对象。第一个是字符串,基本上是键,第二个是返回数组的函数的输出。然后,我将结果管道化到Format表中,并按string属性进行分组。我想看到的是数组属性的每个元素在输出中的一个单独的行上。相反,Format Table将数组显示在一行上 有没有办法格式化输出,使数组属性的每个元素都显示在单独的行上 下面是一些说明问题的代码: function Get-Result([string]$lookup) { if ($lookup -eq "first")

我正在创建一个具有两个属性的自定义对象。第一个是字符串,基本上是键,第二个是返回数组的函数的输出。然后,我将结果管道化到Format表中,并按string属性进行分组。我想看到的是数组属性的每个元素在输出中的一个单独的行上。相反,Format Table将数组显示在一行上

有没有办法格式化输出,使数组属性的每个元素都显示在单独的行上

下面是一些说明问题的代码:

function Get-Result([string]$lookup)
{
    if ($lookup -eq "first")
    {
        return @("one", "two")
    }
    else
    {
        return @("three")
    }
}

$a = "first", "second"

$a | 
    Select-Object @{Name="LookupValue"; Expression={$_}}, `
        @{Name="Result"; Expression={Get-Result $_}} | 
    Format-Table -GroupBy LookupValue
这就是它的输出:

   LookupValue: first

LookupValue Result    
----------- ------    
first       {one, two}


   LookupValue: second

LookupValue Result
----------- ------
second      three 
我想看到的是:

   LookupValue: first

LookupValue Result    
----------- ------    
first       one  
first       two    


   LookupValue: second

LookupValue Result
----------- ------
second      three 

一种方法是:

function Get-Result([string]$lookup) {
    if ($lookup -eq "first") {
        Write-Output @( 
            @{ LookupValue=$lookup; Result="one" },
            @{ LookupValue=$lookup; Result="two" }
        )
    }
    else {
        Write-Output @(
            @{ LookupValue=$lookup; Result="three" }
        )
    }
}

"first", "second" | % {  Get-Result($_) } | % { [PSCustomObject]$_ } | Format-Table LookupValue, Result -GroupBy LookupValue
输出:

    LookupValue: first

LookupValue Result
----------- ------
first       one   
first       two   


   LookupValue: second

LookupValue Result
----------- ------
second      three 

Format cmdlet
不会创建不存在的对象。您的示例有一个包含数组的结果值。如果您不希望它是一个数组,而是两个不同对象的一部分,那么您需要以某种方式将其拆分,或者更改创建过程以生成多个/缺少的对象

前者通过添加另一个内部循环来处理多个“结果”

因此,对于
Get Result
的每个输出,都会创建一个新对象,引用管道中的当前
LookupValue
,该值由
$element
表示

这有点笨重,所以我还想添加一个梯形图的示例,我们可以在其中更改您的创建过程

function Get-Result{
    param(
        [Parameter(
            ValueFromPipeline)]
        [string]$lookup
    )

    process{
        switch($lookup){
            "first"{
                [pscustomobject]@{LookupValue=$lookup;Result="one"},
                [pscustomobject]@{LookupValue=$lookup;Result="two"}
            }
            default{
                [pscustomobject]@{LookupValue=$lookup;Result="three"}
            }

        }
    }
}

$a = "first", "second"
$a | Get-Result | Format-Table -GroupBy LookupValue
创建一个函数,该函数接受管道输入并基于
$lookup
吐出pscustomobjects。我在这里使用了一个开关,以防您的实际应用比您在示例中显示的条件更多

注 为了使
-GroupBy
工作,应该对数据集进行排序。因此,如果您遇到真实数据显示不正确的问题。。。。先分类

function Get-Result{
    param(
        [Parameter(
            ValueFromPipeline)]
        [string]$lookup
    )

    process{
        switch($lookup){
            "first"{
                [pscustomobject]@{LookupValue=$lookup;Result="one"},
                [pscustomobject]@{LookupValue=$lookup;Result="two"}
            }
            default{
                [pscustomobject]@{LookupValue=$lookup;Result="three"}
            }

        }
    }
}

$a = "first", "second"
$a | Get-Result | Format-Table -GroupBy LookupValue