Support Forums

We are currently experiencing failures on the SAPIEN store and are working hard to rectify the situation. In the meantime, please DO NOT place an order through the store. Instead, please contact us directly at sales@sapien.com. We apologize for any inconvenience.

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.
User avatar
alwo23@hotmail.com
Posts: 27
Joined: Sat Sep 28, 2013 1:56 am

Compare two hashtables

Postby 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: 10501
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Postby 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

Postby 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: 10501
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Postby 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

Postby 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: 10501
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Postby jvierra » Wed Jan 13, 2016 2:33 am

Here is how to compare hash tables for all keys:

$replace = @{
l = "Kempten"
Description = "Held des Tages"
sn = "Proper"
StreetAddress = "Landstrasse"
wWWHomePage = "www.google.de"
mail = "Test@contoso.com"
}
$propkey = @{
l = '$props.l'
Description = '$props.Description'
sn = 'Proper'
StreetAddress = '$props.StreetAddress'
wWWHomePage = '$props.wWWHomepage'
mail= '$props.mail'
}
 
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

Postby 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: 10501
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Postby 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

Postby 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: 10501
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Compare two hashtables

Postby 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.

Return to “Windows PowerShell”

Who is online

Users browsing this forum: No registered users and 5 guests