![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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 Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| 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)"}}
Thank you PowerShell wizards! |
My System Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| Vista Ultimate 32, Ultimate 64, Server 2008 | 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])" 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Compare-Object and Get the name of object/File? | PowerShell | |||
| insert object in pipe | PowerShell | |||
| Testing object arrays using Compare-Object and -contains | PowerShell | |||
| Adding canonical aliases for Compare-Object, Measure-Object, New-Object | PowerShell | |||