none
Help with a the Compare-Object cmdlet

    Question

  • We are creating Distribution Lists for a mailbox we have.  It's all External Contacts so I have to first create the DL's.  Create the Contacts.  Then Add the contacts to specific DLs.  My issue is maintaining this contact list.  We will have over 10,000 in there at first, and it will continue to grow unless we maintain it.  So I want to compare the file I am getting(which contains all the needed contacts) with the current contacts.  Spit out the differences.

    Here is what I have so far:

    #Import External Contacts - This is just creating the contacts then marking them as hidden.

    Import-CSV "E:\DistributionLists\pies_ec.csv" |%{New-MailContact -Name $_.Name -ExternalEmailAddress $_.ExternalEmailAddress}
    Import-CSV "E:\DistributionLists\pies_ec.csv" |%{Set-MailContact -Identity $_.Name -HiddenFromAddressListsEnabled $true}


    #Print CSV of current External Contacts - Here is one of my issues.  This prints out the CSV in a format that is not the same as how I am receiving my CSV.  So to compare them is very difficult.  I need to find out how to edit that format.

    Get-MailContact | select alias,externalemailaddress | Export-Csv c:\ec.csv



    #Compare Current Contacts with Export CSV, and export a CSV with the differences - What I would like to do is output one csv that contains the address that are missing from one list(the address that need deleted).  And another CSV with address that are missing from the other list(addresses that need added).  That way instead of trying to add 10k addresses everyday and getting 9,995 errors, we just add the diff....and delete the other diff.

    Compare-Object $(Get-Content E:\DistributionLists\pies_ec.csv) $(Get-Content C:\ec.csv) | Export-Csv c:\ECdiff.csv


    I hope this makes sense.


    Friday, May 04, 2012 7:23 PM

Answers

  • I want to make this output:

    #TYPE Selected.System.Management.Automation.PSCustomObject
    "Alias","ExternalEmailAddress"
    "test","SMTP:testuser@gmail.com"

    look like this--

    Name,ExternalEmailAddress
    test,testuser@gmail.com

    Use -NoTypeInformation with Export-Csv to get rid of the first line.  The quotes you are stuck with, as this is the standard csv format, and that's what Export-Csv does.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Yeah I think I can get around the quotes by just making the guy giving me the initial CSV give it to me in that format. 

    So I just need a noob question answered then.  How can I make it only read lines with "<=" or "=>".  

    if the line contains <=...do these commands...if it contains => do these ones...  I feel like that's really easy, I'm just not very good at powershell.

    No need to make the guy provide the csv in the required format (and have to rely on it being done consistently), just make your own copy on the fly like this:

       $inputData = Import-CSV "E:\DistributionLists\pies_ec.csv"
       $inputData | export-csv "E:\DistributionLists\CompareCopy.csv" -NoTypeInformation
       $inputData |
          %{New-MailContact -Name $_.Name -ExternalEmailAddress $_.ExternalEmailAddress
          Set-MailContact -Identity $_.Name -HiddenFromAddressListsEnabled $true}

     


    Al Dunbar

    Monday, May 07, 2012 4:22 PM

All replies

  • I hope this makes sense:

    $obj1 = 1..5
    $obj2 = 3..8
    $uniqueToObj1 = Compare-Object $obj1 $obj2 | ? { $_.sideindicator -eq '<=' }
    $uniqueToObj2 = Compare-Object $obj1 $obj2 | ? { $_.sideindicator -eq '=>' }


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Friday, May 04, 2012 7:39 PM
  • I want to make this output:

    #TYPE Selected.System.Management.Automation.PSCustomObject
    "Alias","ExternalEmailAddress"
    "test","SMTP:testuser@gmail.com"

    look like this--

    Name,ExternalEmailAddress
    test,testuser@gmail.com

    And I think I can get the different lists I am request above, like this:(right?)

    Compare-Object -referenceobject $(Get-Content E:\DistributionLists\pies_ec.csv) -differenceobject $(Get-Content C:\ec.csv) | Export-Csv c:\pies_del.csv

    Compare-Object -differenceobject $(Get-Content E:\DistributionLists\pies_ec.csv) -referenceobject $(Get-Content C:\ec.csv) | Export-Csv c:\pies_add.csv



    Friday, May 04, 2012 7:40 PM
  • I want to make this output:

    #TYPE Selected.System.Management.Automation.PSCustomObject
    "Alias","ExternalEmailAddress"
    "test","SMTP:testuser@gmail.com"

    look like this--

    Name,ExternalEmailAddress
    test,testuser@gmail.com

    Use -NoTypeInformation with Export-Csv to get rid of the first line.  The quotes you are stuck with, as this is the standard csv format, and that's what Export-Csv does.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Friday, May 04, 2012 7:43 PM
  • I want to make this output:

    #TYPE Selected.System.Management.Automation.PSCustomObject
    "Alias","ExternalEmailAddress"
    "test","SMTP:testuser@gmail.com"

    look like this--

    Name,ExternalEmailAddress
    test,testuser@gmail.com

    Use -NoTypeInformation with Export-Csv to get rid of the first line.  The quotes you are stuck with, as this is the standard csv format, and that's what Export-Csv does.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Yeah I think I can get around the quotes by just making the guy giving me the initial CSV give it to me in that format. 

    So I just need a noob question answered then.  How can I make it only read lines with "<=" or "=>".  

    if the line contains <=...do these commands...if it contains => do these ones...  I feel like that's really easy, I'm just not very good at powershell.

    Friday, May 04, 2012 7:55 PM
  • Should this work?  Right now it's not outputting the correct information.

    $obj1 = Import-csv E:\DistributionLists\CurrentContacts.csv
    $obj2 = Import-csv E:\DistributionLists\PiesContacts.csv
    Compare-Object $obj1 $obj2 | ? { $_.sideindicator -eq '<=' } | export-csv -notypeinformation E:\DistributionLists\pies_del.csv
    Compare-Object $obj1 $obj2 | ? { $_.sideindicator -eq '=>' } | export-csv -notypeinformation E:\DistributionLists\pies_add.csv

    Monday, May 07, 2012 2:34 PM
  • In what way are the results incorrect?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Monday, May 07, 2012 3:16 PM
  • We are creating Distribution Lists for a mailbox we have.  It's all External Contacts so I have to first create the DL's.  Create the Contacts.  Then Add the contacts to specific DLs.  My issue is maintaining this contact list.  We will have over 10,000 in there at first, and it will continue to grow unless we maintain it.  So I want to compare the file I am getting(which contains all the needed contacts) with the current contacts.  Spit out the differences.

    Here is what I have so far:

    #Import External Contacts - This is just creating the contacts then marking them as hidden.

    Import-CSV "E:\DistributionLists\pies_ec.csv" |%{New-MailContact -Name $_.Name -ExternalEmailAddress $_.ExternalEmailAddress}
    Import-CSV "E:\DistributionLists\pies_ec.csv" |%{Set-MailContact -Identity $_.Name -HiddenFromAddressListsEnabled $true}


    #Print CSV of current External Contacts - Here is one of my issues.  This prints out the CSV in a format that is not the same as how I am receiving my CSV.  So to compare them is very difficult.  I need to find out how to edit that format.

    Get-MailContact | select alias,externalemailaddress | Export-Csv c:\ec.csv



    #Compare Current Contacts with Export CSV, and export a CSV with the differences - What I would like to do is output one csv that contains the address that are missing from one list(the address that need deleted).  And another CSV with address that are missing from the other list(addresses that need added).  That way instead of trying to add 10k addresses everyday and getting 9,995 errors, we just add the diff....and delete the other diff.

    Compare-Object $(Get-Content E:\DistributionLists\pies_ec.csv) $(Get-Content C:\ec.csv) | Export-Csv c:\ECdiff.csv


    I hope this makes sense.



    just a minor suggestion. Your code imports the .csv twice, once for each of the two cmdlets you need to run. I'd suggest merging these two commands into a single compound statement:

    Import-CSV "E:\DistributionLists\pies_ec.csv" |
      %{New-MailContact -Name $_.Name -ExternalEmailAddress $_.ExternalEmailAddress
      Set-MailContact -Identity $_.Name -HiddenFromAddressListsEnabled $true}


    Al Dunbar

    Monday, May 07, 2012 4:15 PM
  • I want to make this output:

    #TYPE Selected.System.Management.Automation.PSCustomObject
    "Alias","ExternalEmailAddress"
    "test","SMTP:testuser@gmail.com"

    look like this--

    Name,ExternalEmailAddress
    test,testuser@gmail.com

    Use -NoTypeInformation with Export-Csv to get rid of the first line.  The quotes you are stuck with, as this is the standard csv format, and that's what Export-Csv does.

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Yeah I think I can get around the quotes by just making the guy giving me the initial CSV give it to me in that format. 

    So I just need a noob question answered then.  How can I make it only read lines with "<=" or "=>".  

    if the line contains <=...do these commands...if it contains => do these ones...  I feel like that's really easy, I'm just not very good at powershell.

    No need to make the guy provide the csv in the required format (and have to rely on it being done consistently), just make your own copy on the fly like this:

       $inputData = Import-CSV "E:\DistributionLists\pies_ec.csv"
       $inputData | export-csv "E:\DistributionLists\CompareCopy.csv" -NoTypeInformation
       $inputData |
          %{New-MailContact -Name $_.Name -ExternalEmailAddress $_.ExternalEmailAddress
          Set-MailContact -Identity $_.Name -HiddenFromAddressListsEnabled $true}

     


    Al Dunbar

    Monday, May 07, 2012 4:22 PM