问题
I'm trying to create a function that compares if the beginning of an input string matches one of the possibilities generated from the combinations in multiple System.Arrays
. The result of this function will be a returned object with the input GroupName
we received and if it's a valid name or not $true/$false
.
Array1:
Country
-------
BEL
NLD
Array2:
Color
----
Green
Red
Array3:
Type Object
---- ------
Fruit Banana
Veggetables Aubergine
Fruit Appel
Veggetables Carrot
Fruit Peer
Generated list in the function to check validity against:
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
The code I have already is to create the object. But I don't know the best way on how to generate this list in the function and fill the value 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
}
}
}
回答1:
So I realize that you already accepted an answer, but I thought I'd offer an alternative solution. Using a RegEx match against the group name format "Country Color-Type-Object" and the -Contains operator you don't even need to generate the whole list of possible names.
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}
}
}
}
}
That will output everything piped into it with the GroupName and Valid properties, so if you want to only process things that are valid do something like:
"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}
That will just output the valid names:
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
This way you can run a report after the fact with Where{!$_.Valid}
and find out which ones failed and form a list of rejects.
GroupName Valid
--------- -----
BEL Green-Fruit-Grape False
回答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"
}
}
}
}
来源:https://stackoverflow.com/questions/25199659/powershell-how-to-generate-multiple-combinations