Recursively Export Distribution List Users Exchange 365

I have had a few requests for this and have not been able to produce so im turning to some experts:

We have a distribution list called "all companies". In this list are more distribution lists named "all boston staff", "all New Yrok Staff" etc. I need a way to export to a csv, all of the members of the "all companies" distribution list that has all of the members of the child lists. I know this can be done in powershell but every port I have found has fallen flat, given an error or other.

We are using office365 and not an on premise exchange solution.

Thanks in advance.
jthibeaultAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
Get-DistributionGroupMember command should work with Exchange Online too..

Try..
filter Get-GroupRecurse {
	if($_.RecipientType -match "Group") {
	Get-DistributionGroupMember $_.Name | Get-GroupRecurse
	} else {
	$_
	}
}
Get-DistributionGroupMember "all companies" | Get-GroupRecurse | Select -Unique | Export-Csv C:\report.csv -NTI

Open in new window

0
James HodgeManaging DirectorCommented:
Hello and thanks for your question.

You can use the Get-DistributiongroupMember command with Office 365 Exchange Online.

Here is the official article from Microsoft:
http://gallery.technet.microsoft.com/office/Get-Distribution-Group-4f0ad588

Hopefully you can follow this ok, but if you need a hand let me know.

Good Luck

James
0
jthibeaultAuthor Commented:
@subsun

I get the following error: Pipeline not executed because a pipeline is already existing. Pipelines cannot be executed concurrently.

The CSV is created with only one user.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

SubsunCommented:
I just tested it.. but it's not throwing error for me..

Anyways try this modified code and see if you get any error...


filter Get-GroupRecurse {
	if($_.RecipientType -match "Group") {
	Get-DistributionGroupMember $_.Name | Get-GroupRecurse
	} else {
	$_
	}
}
$(Foreach ($member in Get-DistributionGroupMember "all companies"){
$member | Get-GroupRecurse | Select -Unique}) | Export-Csv C:\report.csv -NTI

Open in new window

0
jthibeaultAuthor Commented:
@James_Hodge

All works well except I dont get a list of users in the CSV, only a column that has this: 27c87ef9bbda4f709f6b4002fa4af63c

Not sure whats up with that.
0
jthibeaultAuthor Commented:
@subsun:

Same deal unfortunately. Although this time I get 14 users and the error only appears twice instead of four times. Should I send you the entire script im running?
0
SubsunCommented:
So did you modify/add anything in the code which I posted?
0
jthibeaultAuthor Commented:
@subsun:

Its just the normal stuff we have in every script to connect to E365:


Set-ExecutionPolicy Unrestricted -Force
chdir c:\
$LiveCred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred -Authentication Basic –AllowRedirection
Import-PSSession $Session
filter Get-GroupRecurse {
      if($_.RecipientType -match "Group") {
      Get-DistributionGroupMember $_.Name | Get-GroupRecurse
      } else {
      $_
      }
}
$(Foreach ($member in Get-DistributionGroupMember "drg@dresourcesgroup.com"){
$member | Get-GroupRecurse | Select -Unique}) | Export-Csv C:users\jthibeault\desktop\report.csv -NTI
0
SubsunCommented:
Hmm.. ok didn't see any issues..Also I a not able to reproduce the issue.. I will try to come with something else when i get a chance..
0
SubsunCommented:
Try this and see if it gives you same error..
filter Get-GroupRecurse {
	if($_.RecipientType -match "Group") {
	Get-DistributionGroupMember $_.Name | Get-GroupRecurse
	} else {
	$_
	}
}

$Report = @()

Foreach ($member in Get-DistributionGroupMember "all companies"){
$Report += $member | Get-GroupRecurse | Select -Unique
}

$Report | Export-Csv C:\report.csv -NTI

Open in new window


If above is not working then you may also try the code from following article. The code is for Exchange 2010 however you can comment out Add-PSSnapin line and use it..

https://christopherlaw.com/2012/10/get-recursive-group-membership-of-distribution-group-powershell/
0
jthibeaultAuthor Commented:
@Subsun

Thanks for your continued effort. I ran your changes as suggested and I get the following error: "Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently"

I get this four times followed by a csv that only has a handful of users. I tried inserting the alias of the Distribution List in quotes and the Email Address. Each time I get the same error and the same incomplete csv file. When I use the display name I get: "The script failed due to a cal depth overflow. The call Depth reached 1001 and the maximum is 1000."

I then tried the script in the link you sent but im not sure I get that one right either as  I get the following error: "The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

I quoted out section #16. I have tried removing the else from section 32. That gives no error but the csv has nothing in it.

******EDIT: Looks like I didnt remove the numbers from the post. After removing the numbers the script works from your link.

I still have an issue where its not outputting to a csv even though i put in the command: export-csv -path "C:\report.csv" at the end i just get: "cmdlet export-csv at command pipeline position 1"
"Supply Values for the following parameters: InputObject:"

Any suggestions on how I can edit that script to output to a csv?
0
SubsunCommented:
I just updated the last line of the code from the article..
Try..
## Install Excahnge Snapin
    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue; #2010

## Clear the screen
    cls

## Set Variables:
    $group = "#Every Employee (NC)"
    $members = New-Object System.Collections.ArrayList

## Create the Function
    function getMembership($group) {
        $searchGroup = Get-DistributionGroupMember $group -ResultSize Unlimited
        foreach ($member in $searchGroup) {
            if ($member.RecipientTypeDetails-match "Group" -and $member.DisplayName -ne "") {
                getMembership($member.DisplayName)
                }           
            else {
                if ($member.DisplayName -ne "") {
                    if (! $members.Contains($member.DisplayName) ) {
                        $members.Add($member.DisplayName) >$null
                        }
                    }
                }
            }
        }

## Run the function
    getMembership($group)

## Output results to screen
    $members | Select @{N="UserName";E={$_}} | Export-Csv c:\report.csv -NTI

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.