0

I have 2 hash tables :

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

I need to get this kind of result:

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

or

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

I tried :

$tempLoca = @()

foreach ($value in $Profil.values) {
    if($Localisation.Contains($value)) {
        $tempLoca = $Localisation.Contains($value),$Profil.key
    }
}

But I get :

$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).

Do you have an idea? Thanks

2
  • 1
    Check out this answer. It's similar case so you should be able to adapt the solution to your needs. Commented Aug 13, 2018 at 16:30
  • 1
    Actually it might be the same case as it's also your question... Commented Aug 13, 2018 at 16:45

2 Answers 2

3

Try this:

[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

It will output

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

Note that I combine the OU values from the $Localisation hash with a semicolon ; to distinct them from the values themselves. If that is not what you want, just replace ($result -join '; ') with ($result -join ', ')

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explication ! It's great !
3

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

Sample output:

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

And grouped:

$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

1 Comment

I can't use the first solution : [System.String[] doesn't contain method "Trim". I use Powershell 2.0 And for the second (Grouped). Nothing happen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.