locked
List all Distributin groups a contacts belongs to ( i have contacts in csv file) RRS feed

  • Question

  • Hello Experts,

    Need help in below script

    I have a csv file with 200 contacts. This is hybrid environment and contacts are synced to O365.

    csv file format is:

    contact

    contact1.domain.com

    contact2.domain2.com

    I need to know if these contacts belong to any distribution group/s , if yes, should export the name of contact, email address and group name and group primary smtp address and then list them in output csv file

    I am trying below script

                            

    $Collection = @()
                    $contacts = import-csv C:\UPN1\test.csv

                    ForEach($contact in $contacts) {

                                    $DN=$contact.DistinguishedName
                                    $Filter = "Members -eq '$DN'"
                                    $groups = Get-DistributionGroup -ResultSize unlimited -Filter $Filter 
                                    $outObject = "" | Select "Full Name","Email","Groups"
                                    $outObject."Full Name" = $contact.DisplayName
                                    $outObject."Email" = $contact.WindowsEmailAddress
                                    $outObject."Groups" = (@($groups) -join ',') 
                                    $collection += $outObject
                    }
                    $Collection | export-csv C:\UPN1\latest1.csv

    However i am getting error:

    WARNING: An unexpected error has occurred and a Watson dump is being generated: Object reference not set to an instance of an object.
    Object reference not set to an instance of an object.
        + CategoryInfo          : NotSpecified: (:) [Get-DistributionGroup], NullReferenceException
        + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.Exchange.Management.RecipientTasks.GetDistributionGroup
        + PSComputerName        : outlook.office365.com


    • Edited by shah777 Sunday, March 15, 2020 6:16 PM
    Sunday, March 15, 2020 6:15 PM

All replies

  • Your CSV does not have a field called DistinguishedName.


    \_(ツ)_/

    Sunday, March 15, 2020 7:04 PM
  • Thanks Jrv

    please suggest exactly how csv should be created

    what fields and values i nee to put in csv fle

    and what about script..is there any changes

    i want to run it to get results

    this is very urgent

    Monday, March 16, 2020 10:15 AM
  • help import-csv -online

    https://en.wikipedia.org/wiki/Comma-separated_values

    We cannot design and write your script for you.  You will have to learn enough to decide what you need to put in your file and how to do it.  The links above will help you get a basic understanding of what these things are and how to use them.

    To learn basic PowerShell start here:

    Also review all of the following:

    Learning to script properly with PowerShell


    \_(ツ)_/


    • Edited by jrv Monday, March 16, 2020 10:21 AM
    Monday, March 16, 2020 10:21 AM
  • By now you should have learned that you must post code correctly in this forum.  Please edit your original post and post correctly formatted code according to the following instructions:


    \_(ツ)_/

    Monday, March 16, 2020 10:23 AM
  • This would be the correct way to write your code:

    import-csv C:\UPN1\test.csv |
        ForEach-Object{
            Get-DistributionGroup $_.DistinguishedName
        } |
        Select Name, DisplayName,Email |
        export-csv C:\UPN1\latest1.csv
    


    \_(ツ)_/

    Monday, March 16, 2020 10:27 AM
  • Thanks Jrv for all your suggestions.
    I will go through links you suggested

    Give me sometime

    for now i need this piece of code working
    Your code is not working as expected

    its giving hundreds of groups which is wrong. in csv file i have kept only two contacts
    if i run command individually against them, they are members of two groups in o365

    Your code above is giving me hundreds of groups
    please advise
    Monday, March 16, 2020 1:05 PM
  • I cannot design the code for you as you are not asking any kind of logical question.  You need to learn the basics in order to ask a question.  Until you know what to ask you will have issues.

    Please learn the basics and then try to ask your question.  If this is critical then you will have to hire a consultant to do it for you.


    \_(ツ)_/

    Monday, March 16, 2020 1:10 PM
  • who else can help here

    I am asking a question to experts

    Expert is expected to know the code and help/suggest

    I posted my requirement and what i need. if you dont want to help, thats ok

    appreciate your time so far

    Monday, March 16, 2020 6:14 PM
  • We know the code we just can't understand your question. 

    You have a CSV file that contains one field called "DistinguishedName" and you are asying that the code I posted returns all groups.  That is clearly not possible.  The reason you are reporting something impossible is because you are doing something that we cannot see and you are not giving us the right information to understand what you  are doing.

    It is your responsibility to know enough to ask a clear question.  Without that we cannot even begin to know how to help you.

    Start by reading the following carefully:

    After reading it try to ask a question that gives correct and accurate information that is complete enough to define the problem.


    \_(ツ)_/

    Monday, March 16, 2020 7:27 PM