Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ensuring two properties are the same across two lists

Posted on 2015-02-03
16
Medium Priority
?
139 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 1500 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Assisted Solution

by:footech
footech earned 500 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
 
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 1500 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 1500 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

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

972 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