Solved

Ensuring two properties are the same across two lists

Posted on 2015-02-03
16
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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 antispam), the admini…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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