Solved

Ensuring two properties are the same across two lists

Posted on 2015-02-03
16
106 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 40

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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