问题
I have two hash tables and I need to compare them. Let me explain my problem :
[hashtable]$User = @{
"Jack" = "AdminLA, AdminUSA";
"John" = "AdminAustralia";
"Sarah" = "AdminIceland";
"Arnold" = "AdminUSA";
"Maurice" = "AdminAustralia, AdminCanada";
}
[hashtable]$Profil = @{
"AdminLA" = "P1";
"AdminIceland" = "P2";
"AdminUSA" = "P3";
"AdminCanada" = "P4";
"AdminAustralia" = "P5" ;
"AdminCroatia" = "P6";
}
I want to have this kind of result :
Key Value
--- -----
Jack P1, P3
John P5
Sarah P2
Arnold P3
Maurice P5, P4
Actually, I have only one value (I haven't succeeded to have multiple values. For example Jack must have P1 and P3 and I have only P1).
How can I fix it?
I have already tried:
$User.GetEnumerator() | select Key, @{n='Value'; e={$Profil[$_.Value]}}
and
$User.GetEnumerator() | %{[PSCustomObject]@{aKey=$_.Key;bValue=$Profil[$_.Value]}}
Any idea?
回答1:
Use this expression
$User.GetEnumerator() | Select-Object Key, @{name='Value'; expression={($_.Value -split ", " | Foreach-Object {$Profil[$_]}) -join ", "}}
This basically creates an array of input values, get the values from $Profil
for each element and then creates a string from these values.
来源:https://stackoverflow.com/questions/51539849/join-two-hashtables-to-make-one