Powershell如何生成多个组合

时间:2014-08-08 09:01:17

标签: arrays powershell generate-series

我试图创建一个函数来比较输入字符串的开头是否与多个System.Arrays中的组合生成的一种可能性相匹配。此函数的结果将是返回的对象,我们收到输入GroupName,如果它是有效名称$true/$false

数组1:

Country
-------
BEL
NLD

ARRAY2:

Color
----
Green
Red

ARRAY3:

Type            Object
----            ------
Fruit           Banana
Veggetables     Aubergine
Fruit           Appel
Veggetables     Carrot
Fruit           Peer

在功能中生成的列表,用于检查有效性:

BEL Green-Fruit-Banana
BEL Green-Fruit-Appel
BEL Green-Fruit-Peer
BEL Green-Vegetables-Aubergine
BEL Green-Vegetables-Carrot
NLD Green-Fruit-Banana
NLD Green-Fruit-Appel
NLD Green-Fruit-Peer
NLD Green-Vegetables-Aubergine
NLD Green-Vegetables-Carrot
BEL Red-Fruit-Banana
BEL Red-Fruit-Appel
BEL Red-Fruit-Peer
BEL Red-Vegetables-Aubergine
BEL Red-Vegetables-Carrot
NLD Red-Fruit-Banana
NLD Red-Fruit-Appel
NLD Red-Fruit-Peer
NLD Red-Vegetables-Aubergine
NLD Red-Vegetables-Carrot

我已经编写的代码是创建对象。但我不知道如何在函数中生成此列表的最佳方法并填充值Valid

Function Compare-Names {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
        [string[]]$GroupName
    )
    PROCESS {
        # Generate all options
        <# Fill Valid $true or false here #>

        foreach ($Group in $GroupName) {
            $obj = New-Object -TypeName psobject -Property ([ordered] @{
                'GroupName' = $Group;
                'Valid' = $Valid;
            })
        Write-Output $obj
        }
    }
}

2 个答案:

答案 0 :(得分:2)

所以我意识到你已经接受了答案,但我认为我提供了另一种解决方案。使用RegEx匹配组名称格式&#34; Country Color-Type-Object&#34;和-Contains运算符甚至不需要生成可能名称的完整列表。

Function Compare-Names {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
        [string[]]$GroupName
    )
    BEGIN {
        $Array1 = "BEL","NLD"|%{[pscustomobject]@{Country=$_}}
        $Array2 = "Green","Red"|%{[pscustomobject]@{Color=$_}}
        $Array3 = ("Fruit","Banana"),("Vegetables","Aubergine"),("Fruit","Appel"),("Vegetables","Carrot"),("Fruit","Peer")|%{[pscustomobject]@{Type=$_[0];Object=$_[1]}}
        [RegEx]$RegEx = "(.+?) (.+?)-(.+?)-(.+?)$"
    }
    PROCESS {
        ForEach($Group in @($GroupName)){
        $NameSplit = $regex.Match($Group) | Select -ExpandProperty Groups
            [PSCustomObject][ordered] @{
                'GroupName' = $Group
                'Valid' = If($Array1.Country -contains $NameSplit[1] -and $Array2.Color -contains $NameSplit[2] -and $Array3.Type -contains $NameSplit[3] -and $Array3.Object -contains $NameSplit[4]){$true}else{$false}
            }
        }
    }
}

这将输出使用GroupName和Valid属性传送到其中的所有内容,因此如果您只想处理有效的内容,请执行以下操作:

"BEL Green-Fruit-Banana",
"BEL Green-Fruit-Appel",
"BEL Green-Fruit-Peer",
"BEL Green-Vegetables-Aubergine",
"BEL Green-Vegetables-Carrot",
"NLD Green-Fruit-Banana",
"BEL Green-Fruit-Grape" | Compare-Names | Where{$_.Valid}

只输出有效名称:

GroupName                      Valid
---------                      -----
BEL Green-Fruit-Banana          True
BEL Green-Fruit-Appel           True
BEL Green-Fruit-Peer            True
BEL Green-Vegetables-Aubergine  True
BEL Green-Vegetables-Carrot     True
NLD Green-Fruit-Banana          True

通过这种方式,您可以在Where{!$_.Valid}之后运行报告,找出哪些报告失败并形成拒绝列表。

GroupName             Valid
---------             -----
BEL Green-Fruit-Grape False

答案 1 :(得分:1)

$Countries = @('BEL', 'NLD')
$Colors = @('Green', 'Red')
$Objects = @{ 'Fruit' = @('Banana', 'Apple'); 'Vegetable' = @('Aubergine', 'Carrot') }

$countries | %{
    $country = $_

    $Colors | %{
        $color = $_

        $Objects.Keys | %{
            $key = $_
            $values = $Objects.$key

            $values | %{
                $value = $_

                Write-Host "$country $color-$key-$value"
            }
        }
    }
}
相关问题