Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Windows 7 Forum Vista Tutorials Tags

Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Pipe compare-object results into something useable

Reply
 
LinkBack Thread Tools Display Modes
Old 10-07-2008   #1 (permalink)
Member


Join Date: Oct 2008
Vista Ultimate 32, Ultimate 64, Server 2008
 
 

Pipe compare-object results into something useable

I'm sure this can be done but I don't know where to start...

This will eventually make it's way into Exchange Management Shell. What I need to do is compare two text files that contain email addresses, one per line.

If file A contains an email address that file B doesn't have, add it as a contact in AD.
If file B has an email address that isn't in file A, remove its contact from AD.
Then overwrite file B with file A so the next day's run will be in sync.

I'm trying to build distribution lists that contain external addresses. In Exchange 2007 you need to have a contact in your AD to add the user to a distribution list.

I'm trying to automate the whole create/delete a contact in AD. Our ERP system which runs on Sun OS generates the source text file with email addresses nightly (whether there has been a change or not). If I could use this output and compare its content with a second file I may be able to accomplish what I want.

Can this be done?
Todd

My System SpecsSystem Spec
Old 10-07-2008   #2 (permalink)
PaulChavez
Guest


 
 

RE: Pipe compare-object results into something useable

Here is a sample of how to use compare-object, just replace the string with a
call to a function that does the desired operation.

compare-object (gc list1.txt) (gc list2.txt) |
%{
if ($_.SideIndicator -eq "<=") {"$($_.InputObject) exists in file 1"}
if ($_.SideIndicator -eq "=>") {"$($_.InputObject) exists in file 2"}
}


"daystrom" wrote:
Quote:

>
> I'm sure this can be done but I don't know where to start...
>
> This will eventually make it's way into Exchange Management Shell. What
> I need to do is compare two text files that contain email addresses, one
> per line.
>
> If file A contains an email address that file B doesn't have, add it as
> a contact in AD.
> If file B has an email address that isn't in file A, remove its contact
> from AD.
> Then overwrite file B with file A so the next day's run will be in
> sync.
>
> I'm trying to build distribution lists that contain external addresses.
> In Exchange 2007 you need to have a contact in your AD to add the user
> to a distribution list.
>
> I'm trying to automate the whole create/delete a contact in AD. Our ERP
> system which runs on Sun OS generates the source text file with email
> addresses nightly (whether there has been a change or not). If I could
> use this output and compare its content with a second file I may be able
> to accomplish what I want.
>
> Can this be done?
> Todd
>
>
> --
> daystrom
>
My System SpecsSystem Spec
Old 10-07-2008   #3 (permalink)
Member


Join Date: Oct 2008
Vista Ultimate 32, Ultimate 64, Server 2008
 
 

Re: Pipe compare-object results into something useable

Thank you! That is a start and I think I can build on that.
My System SpecsSystem Spec
Old 10-07-2008   #4 (permalink)
Arnoud Jansveld
Guest


 
 

Re: Pipe compare-object results into something useable

Just remember to sort your lists first and/or set the SyncWindow parameter,
especially if the lists are long...

Arnoud
--
http://www.jansveld.net/powershell



"daystrom" wrote:
Quote:

>
> Thank you! That is a start and I think I can build on that.
>
>
> --
> daystrom
>
My System SpecsSystem Spec
Old 10-08-2008   #5 (permalink)
Member


Join Date: Oct 2008
Vista Ultimate 32, Ultimate 64, Server 2008
 
 

Re: Pipe compare-object results into something useable

OK,

Let's take this a step further.

I have two CSV files with a header line on each.

The CSV would look something like this:

Name,Email
Mickey Mouse,mmouse@disney.com
Donald Duck,dduck@disney.com

I need to compare the two CSV's. The output needs to retain the header information so I can pass it to another command.

Code:
compare-object (gc list1.csv) (gc list2.csv) | %{if ($_.SideIndicator -eq "<=") {new-mailcontact -ExternalEmailAddress "SMTP:$($_.InputObject)" -Name "$($_.InputObject)"}}
The above does the trick but I would like to add the name field with the proper name and not the email address.

Thank you PowerShell wizards!
My System SpecsSystem Spec
Old 10-08-2008   #6 (permalink)
PaulChavez
Guest


 
 

Re: Pipe compare-object results into something useable

In that case the InputObject property of the comparison is the whole line
from the CSV right? I wouldn't think the code as written below would work
correctly, the email address and name would be set to something like "Mickey
Mouse,mmouse@xxxxxx".

To get a different "fields" you can use the split method like this:
new-mailcontact -ExternalEmailAddress "SMTP:$($_.InputObject.split(",")[0])"
-Name "$($_.InputObject.split(",")[1])"

"daystrom" wrote:
Quote:

>
> OK,
>
> Let's take this a step further.
>
> I have two CSV files with a header line on each.
>
> The CSV would look something like this:
>
> Name,Email
> Mickey Mouse,mmouse@xxxxxx
> Donald Duck,dduck@xxxxxx
>
> I need to compare the two CSV's. The output needs to retain the header
> information so I can pass it to another command.
>
>
> Code:
> --------------------
> compare-object (gc list1.csv) (gc list2.csv) | %{if ($_.SideIndicator -eq "<=") {new-mailcontact -ExternalEmailAddress "SMTP:$($_.InputObject)" -Name "$($_.InputObject)"}}
> --------------------
> The above does the trick but I would like to add the name field with the
> proper name and not the email address.
>
> Thank you PowerShell wizards!
>
>
> --
> daystrom
>
My System SpecsSystem Spec
Old 10-09-2008   #7 (permalink)
Member


Join Date: Oct 2008
Vista Ultimate 32, Ultimate 64, Server 2008
 
 

Re: Pipe compare-object results into something useable

Quote:
PaulChavez
View Post
In that case the InputObject property of the comparison is the whole line
from the CSV right? I wouldn't think the code as written below would work
correctly, the email address and name would be set to something like "Mickey
Mouse,mmouse@xxxxxx".

To get a different "fields" you can use the split method like this:
new-mailcontact -ExternalEmailAddress "SMTP:$($_.InputObject.split(",")[0])"
-Name "$($_.InputObject.split(",")[1])"
Very nice! I had to change the (",") to (',') however. The shell came back with a "Incomplete $(subexpression) sequence in string." error.

This does exactly what I want now. If the contact is on the left side and not the right it will create a mail contact in Exchange. With a little modification if it exists on the right and not left it will remove the contact.

You are a wizard!

Thanks!

Todd
My System SpecsSystem Spec
Reply

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare-Object and Get the name of object/File? akcorr PowerShell 5 06-19-2008 05:15 AM
insert object in pipe William Stacey [C# MVP] PowerShell 3 12-04-2006 12:16 AM
Testing object arrays using Compare-Object and -contains Alex K. Angelopoulos [MVP] PowerShell 2 08-31-2006 05:57 PM
Adding canonical aliases for Compare-Object, Measure-Object, New-Object Alex K. Angelopoulos [MVP] PowerShell 2 05-26-2006 07:58 AM


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53