将哈希表中的相似值与Powershell中的循环进行比较 [英] Compare similar values from hashtable with loop in Powershell
本文介绍了将哈希表中的相似值与Powershell中的循环进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个哈希表:
[hashtable]$Localisation = @{
"Macdo" = "OU=France,OU=Paris";
"BurgerKing" = "OU=USA,OU=LA";
"Quick" = "OU=Japan,OU=Tokyo";
}
[hashtable]$Profil = @{
"Big Mac" = "Macdo";
"Whooper" = "BurgerKing";
"Burger" = "Quick, BurgerKing, Macdo";
"Fries" = "BurgerKing, Macdo";
"Coke" = "Quick, Macdo";
"HappyMeal" = "Macdo";
}
我需要得到以下结果:
"Big Mac" = "OU=France,OU=Paris"
"Whooper" = "OU=USA,OU=LA";
"Burger" = "OU=Japan,OU=Tokyo, OU=USA,OU=LA, OU=France,OU=Paris"
"Fries" = "OU=USA,OU=LA, OU=France,OU=Paris";
"Coke" = "OU=Japan,OU=Tokyo, OU=France,OU=Paris";
"HappyMeal" = "OU=France,OU=Paris";
或
Big Mac = OU=France,OU=Paris
Whooper = OU=USA,OU=LA
Burger = OU=Japan,OU=Tokyo,
OU=USA,OU=LA,
OU=France,OU=Paris
Fries = OU=USA,OU=LA,
OU=France,OU=Paris
Coke = OU=Japan,OU=Tokyo,
OU=France,OU=Paris
HappyMeal = OU=France,OU=Paris
我尝试过:
$tempLoca = @()
foreach ($value in $Profil.values) {
if($Localisation.Contains($value)) {
$tempLoca = $Localisation.Contains($value),$Profil.key
}
}
但是我得到了:
$tempLoca
OU=France,OU=Paris
使用我的代码,我只有最后一个值。我不知道是否需要将值放入数组或哈希表中(因为它们是多个相似的值)。
With my code I have only the last value. I don't know if I need to put my values in array or in hashtable (because they are multiple similar values).
您有想法吗?谢谢
推荐答案
类似于IMO的PowerShell,构建PSCustomObject并将其分组:
An IMO more PowerShell like way, building a PSCustomObject and grouping it:
$ProfileLocalisation = ForEach ($key in $Profil.Keys) {
ForEach ($local in ($Profil.$key -split ',').Trim() ) {
[PSCustomObject]@{
Profile = $key
Localisation = $Localisation.$local
}
}
}
$ProfileLocalisation
样本输出:
Profile Localisation
------- ------------
Big Mac OU=France,OU=Paris
HappyMeal OU=France,OU=Paris
Burger OU=Japan,OU=Tokyo
Burger OU=USA,OU=LA
Burger OU=France,OU=Paris
Whooper OU=USA,OU=LA
Fries OU=USA,OU=LA
Fries OU=France,OU=Paris
Coke OU=Japan,OU=Tokyo
Coke OU=France,OU=Paris
并分组:
$ProfileLocalisation | Group-Object Profile | ForEach-Object {
[PSCustomObject]@{
Profile = $_.Name
Localisations = ($_.Group.Localisation -join ';')
}
}
Profile Localisations
------- -------------
Big Mac OU=France,OU=Paris
HappyMeal OU=France,OU=Paris
Burger OU=Japan,OU=Tokyo;OU=USA,OU=LA;OU=France,OU=Paris
Whooper OU=USA,OU=LA
Fries OU=USA,OU=LA;OU=France,OU=Paris
Coke OU=Japan,OU=Tokyo;OU=France,OU=Paris
这篇关于将哈希表中的相似值与Powershell中的循环进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文