我试图创建一个函数,比较输入字符串的开头是否与多个System.Arrays
中的组合生成的可能性之一相匹配。这个函数的结果将是一个返回的对象,我们接收到的输入是GroupName
,如果它是一个有效的名称或不是$true/$false
。
Array1:
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
}
}
}
我知道你已经接受了一个答案,但我想我应该提供一个替代解决方案。使用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
$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"
}
}
}
}