Solved

Ensuring two properties are the same across two lists

Posted on 2015-02-03
16
105 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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

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.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 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