Compare two hashtables

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Compare two hashtables

Post by alwo23@hotmail.com » Wed Jan 13, 2016 1:44 am

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.

User avatar
jvierra
Posts: 12895
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Post by jvierra » Wed Jan 13, 2016 1:59 am


User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Re: Compare two hashtables

Post by alwo23@hotmail.com » Wed Jan 13, 2016 2:04 am

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.

User avatar
jvierra
Posts: 12895
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Post by jvierra » Wed Jan 13, 2016 2:23 am

You will have to write a custom script to do that with hashes.

User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Re: Compare two hashtables

Post by alwo23@hotmail.com » Wed Jan 13, 2016 2:25 am

Ok. Did you have an example or something else for me?

User avatar
jvierra
Posts: 12895
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Post by jvierra » Wed Jan 13, 2016 2:33 am

Here is how to compare hash tables for all keys:
  1. $replace = @{
  2.  
  3.     l = "Kempten"
  4.  
  5.     Description = "Held des Tages"
  6.  
  7.     sn = "Proper"
  8.  
  9.     StreetAddress = "Landstrasse"
  10.  
  11.     wWWHomePage = "www.google.de"
  12.  
  13.     mail = "Test@contoso.com"
  14.  
  15. }
  16.  
  17. $propkey = @{
  18.  
  19.     l = '$props.l'
  20.  
  21.     Description = '$props.Description'
  22.  
  23.     sn = 'Proper'
  24.  
  25.     StreetAddress = '$props.StreetAddress'
  26.  
  27.     wWWHomePage = '$props.wWWHomepage'
  28.  
  29.     mail= '$props.mail'
  30.  
  31. }
  32.  
  33.  
  34.  
  35. Compare-Object ([PSCustomObject]$replace)  ([PSCustomObject]$propkey) -Property sn, description, StreetAddress

User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Re: Compare two hashtables

Post by alwo23@hotmail.com » Wed Jan 13, 2016 2:43 am

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

User avatar
jvierra
Posts: 12895
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Post by jvierra » Wed Jan 13, 2016 2:50 am

Why do you think you need to do this?

User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Re: Compare two hashtables

Post by alwo23@hotmail.com » Wed Jan 13, 2016 2:52 am

I will write back the different to the active directory. Only the changed thinks a need to be update in the active directory.

User avatar
jvierra
Posts: 12895
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Post by jvierra » Wed Jan 13, 2016 2:57 am

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.

Locked