Page 1 of 1

Compare two hashtables

Posted: Wed Jan 13, 2016 12:44 am
by alwo23@hotmail.com
Hello,
i need help to compare two hashtables:

Hashtable 1:

$replace = @{}
$replace['l'] = "Kempten"
$replace['Description'] ="Held des Tages"
$replace['sn'] = "Proper"
$replace['StreetAddress'] = "Landstrasse"
$replace['wWWHomePage'] = "www.google.de"
$replace['mail'] = "Test@contoso.com"

Hashtable 2
$props = Get-ADUser -Filter { DisplayName -eq "Meister Proper" } -Properties *

$propkey = @{ }
$propkey['l'] = $props.l
$propkey['Description'] = $props.Description
$propkey['sn'] = $props.sn
$propkey['StreetAddress'] = $props.StreetAddress
$propkey['wWWHomePage'] = $props.wWWHomepage
$propkey['mail'] = $props.mail

$output = $Replace| Out-String
Write-Host $output

$output2 = $propkey | Out-String
Write-Host $output2

compare $replace.Values $propkey.Values

Result:

Hashtable 1
Name Value
---- -----
Description Held des Tages
wWWHomePage www.google.de
mail Test@contoso.com
l Kempten
StreetAddress Landstrasse
sn Proper

Hashtable 2
Name Value
---- -----
Description wwww
wWWHomePage
mail MProper@kempten.de
l Kempten
StreetAddress
sn Proper

Compare-Result

InputObject SideIndicator
----------- -------------
{wwww, $null, MProper@kempten.de, Kempten...} =>
{Held des Tages, www.google.de, Test@contoso.com, Kempten...} <=


I need now the different between the hashtables as a new hashtable as example:

Key Value
l Kempten

Thx a lot.

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 12:59 am
by jvierra

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 1:04 am
by alwo23@hotmail.com
Yes i know it, but i didnt have the result what i need.

I need a compare key by key and the different in a new hashtable with key an value filtert by SideIndicator.

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 1:23 am
by jvierra
You will have to write a custom script to do that with hashes.

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 1:25 am
by alwo23@hotmail.com
Ok. Did you have an example or something else for me?

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 1:33 am
by jvierra
Here is how to compare hash tables for all keys:
  1. $replace = @{
  2.     l = "Kempten"
  3.     Description = "Held des Tages"
  4.     sn = "Proper"
  5.     StreetAddress = "Landstrasse"
  6.     wWWHomePage = "www.google.de"
  7.     mail = "Test@contoso.com"
  8. }
  9. $propkey = @{
  10.     l = '$props.l'
  11.     Description = '$props.Description'
  12.     sn = 'Proper'
  13.     StreetAddress = '$props.StreetAddress'
  14.     wWWHomePage = '$props.wWWHomepage'
  15.     mail= '$props.mail'
  16. }
  17.  
  18. Compare-Object ([PSCustomObject]$replace)  ([PSCustomObject]$propkey) -Property sn, description, StreetAddress

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 1:43 am
by alwo23@hotmail.com
Ok i have tested it, but the result is:

l : Kempten
Description : Held des Tages
sn : Proper
StreetAddress :
wwwhomepage :
mail : MProper@kempten.de
SideIndicator : =>

l : Kempten
Description : Held des Tages
sn : Proper
StreetAddress : Landstrasse
wwwhomepage : www.google.de
mail : Test@contoso.com
SideIndicator : <=

There are some properties with the same value but what i need is the different

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 1:50 am
by jvierra
Why do you think you need to do this?

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 1:52 am
by alwo23@hotmail.com
I will write back the different to the active directory. Only the changed thinks a need to be update in the active directory.

Re: Compare two hashtables

Posted: Wed Jan 13, 2016 1:57 am
by jvierra
You do not need to do that. Just use "Replace" with the wholehash. D will only update the fiekds that are different.

Set-AdUser userid -Replace $replace

That is all we ever need to do. Let AD do the comparison for you.