Solved

Ensuring two properties are the same across two lists

Posted on 2015-02-03
16
102 Views
Last Modified: 2015-02-06
I have two lists I'd like to compare.
$FIrst = @{1='abc',2='def'}
$Second = @{1='abc';2='ghi'}

I need to output a list indicating that item 2 was modified from $First to $Second, displaying what the value was for 2, and what it is now.
I believe I can accomplish this via a foreach loop using compare-object, but I wonder if there's not a more effecient method
0
Comment
Question by:sirbounty
  • 8
  • 7
16 Comments
 
LVL 40

Expert Comment

by:Subsun
ID: 40587149
I think it's better to use Foreach here...
$FIrst = @{1='abc';2='def'}
$Second = @{1='abc';2='ghi'}

$Second.keys | %{
 If ($First.($_) -ne $Second.($_)) {
	[PSCustomObject]@{
        Changed = $_
	Old = $First.($_)  
	New = $Second.($_)
	}
 }
}

Open in new window

I have never tried Compare-Object to compare a hash table. but if it is a array then more efficient is Compare-Object
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40587394
The data would ultimately come from a CSV (or one of them would).
I'm basically getting a baseline of some domain data now and want to compare in the future to ensure accounts remain on the same server.
With the csv, I do have it setup to loop through and add it to a hash table - perhaps that's a bit overkill as well?
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 375 total points
ID: 40587423
To compare domain data.. you can save it in to to array's and use Compare-Object to find the difference, which will be quick and efficient.
0
 
LVL 39

Assisted Solution

by:footech
footech earned 125 total points
ID: 40587448
I would think that converting it to a hash table would be unnecessary and would just make it harder to do a compare.  Unless perhaps your file isn't a true .CSV that you can use Import-CSV on?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40587582
I'll have to revisit it tomorrow.
The source file I was given was not a true csv, but reproducing the same commands I was able to generate a csv with no problem...  But, I would need one array to hold the csv data while I'm re-reading the 'live' data.
Thanks for the input so far.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40591852
Type returned (all I need is name and database) is microsoft.exchange.data.directory.management.mailbox.
What do you suggest for outputting as an array?  I need to keep both together (A=B), which is why I was thinking hashtable.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40591903
Are you using Get-Mailbox?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40591972
combined with another - I think it's:
get-mailboxdatabse ... | get-mailbox | select name, database
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:Subsun
ID: 40591997
You should be able to export this to a csv file and later import it or directly save it in to a var. is it not working?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 375 total points
ID: 40592049
For example.. If you have two csv files..
First.csv
Name,Database
Sub,DB001
Sun,DB003

Open in new window

Second.csv
Name,Database
Sub,DB001
Sun,DB002

Open in new window

To compare..
Compare (Import-Csv first.csv) (Import-Csv Second.csv) -Property Name,Database | 
Select Name,Database,@{N="From";E={Switch($_.SideIndicator){"<="{"First"};"=>"{"Second"}}}}

Open in new window

0
 
LVL 67

Author Comment

by:sirbounty
ID: 40592057
Ah, That might work - I'll give it a shot.  Thanks
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40592087
Ok, so I have one csv file that's my baseline.
I want to compare that to today's query - must I dump it into a csv in order to do perform the compare?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 40592095
Well, I retract that question - looks like it may work without doing that...
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 375 total points
ID: 40592113
You can also directly compare the data.. Example..
Compare (Import-Csv first.csv) (Get-mailboxdatabase | get-mailbox | select name, database) -Property Name,Database | 
Select Name,Database,@{N="From";E={Switch($_.SideIndicator){"<="{"First"};"=>"{"Second"}}}}

Open in new window

0
 
LVL 67

Author Comment

by:sirbounty
ID: 40592164
I have a function that tweaks the databases that I'm searching, so I think I'll leave that.

However, if I'm dumping the compare results into a variable, the contents are sorted, so I'm doing that, but have been dropping this in a color contrasting table.

I was going to use this logic to ensure the right css style is applied to the modified version, unless you know of a better way?

foreach ($result in $comparison| sort name) {if ($result.from -ne 'First') {write-host $result.Name,$result.database -ForegroundColor red} else {write-host $result.name,$result.Database}}
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40592923
This is ok, I don't think any other option will make much of a difference..
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article will help you understand what HashTables are and how to use them in PowerShell.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now