1

我正在尝试创建一个函数来比较输入字符串的开头是否与从 multiple 中的组合生成的可能性之一匹配System.Arrays。此函数的结果将是带有输入的返回对象GroupName我们收到的输入以及它是否是有效名称$true/$false

阵列1:

Country
-------
BEL
NLD

数组2:

Color
----
Green
Red

阵列3:

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
        }
    }
}
4

2 回答 2

2
$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"
            }
        }
    }
}
于 2014-08-08T09:29:34.623 回答
2

所以我意识到你已经接受了一个答案,但我想我会提供一个替代解决方案。使用 RegEx 匹配组名称格式“Country Color-Type-Object”和 -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
于 2014-08-08T17:41:42.207 回答