在Powershell中将hashtable与循环中的类似值进行比较

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Powershell中将hashtable与循环中的类似值进行比较相关的知识,希望对你有一定的参考价值。

我有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

用我的代码我只有最后一个值。我不知道是否需要将我的值放在数组或哈希表中(因为它们是多个相似的值)。

你有好主意吗?谢谢

答案

试试这个:

[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";
}


$tempLoca = @()
foreach ($key in $Profil.Keys) {
    $locals = ($Profil.$key -split ',') | ForEach-Object { $_.Trim() }
    $result = @()
    foreach ($item in $locals) {
        if($Localisation.ContainsKey($item)) {
            $result += $Localisation.$item
        }
    } 
    $tempLoca += '"{0}" = "{1}"' -f $key, ($result -join '; ')
}

$temploca

它会输出

"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"

请注意,我将$ Localization哈希值中的OU值与分号;组合在一起,以使它们与值本身区分开来。如果这不是你想要的,只需用($result -join '; ')替换($result -join ', ')

另一答案

IMO更像PowerShell,构建PSCustomObject并对其进行分组:

$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中将hashtable与循环中的类似值进行比较的主要内容,如果未能解决你的问题,请参考以下文章

powershell Powershell函数与lodash _.merge的工作方式类似地合并Hashtables。回答问题“如何合并几个哈希表

如何在 C# 中将 List<object> 转换为 Hashtable?

powershell ConvertTo-Hashtable:Powershell实用程序函数,它将psCustomObject转换为HashTable,以便更好地寻找和迭代

PowerShell 是不是支持 HashTable 序列化?

在 C# 中将 HashTable 转换为字典

如何附加到powershell Hashtable值?