247 lines
7.9 KiB
PowerShell
Raw Normal View History

<#
=============================================================================================
Name: Get distribution group members report
Version: 3.0
Website: o365reports.com
For detailed script execution: https://o365reports.com/2019/05/23/export-office-365-distribution-group-members-csv/
============================================================================================
#>
Param
(
[Parameter(Mandatory = $false)]
[string]$GroupNamesFile,
[switch]$IsEmpty,
[int]$MinGroupMembersCount,
[Nullable[boolean]]$ExternalSendersBlocked = $null,
[string]$UserName,
[string]$Password
)
Function Get_members
{
$DisplayName=$_.DisplayName
Write-Progress -Activity "`n Processed Group count: $Count "`n" Getting members of: $DisplayName"
$Alias=$_.Alias
$EmailAddress=$_.PrimarySmtpAddress
$GroupType=$_.GroupType
$ManagedBy=$_.ManagedBy
$ExternalSendersAllowed=$_.RequireSenderAuthenticationEnabled
if(($ExternalSendersBlocked -ne $null) -and ($ExternalSendersBlocked -ne $ExternalSendersAllowed))
{
$Print=0
}
#Get Distribution Group Authorized Senders
$AcceptMessagesOnlyFrom=$_.AcceptMessagesOnlyFromSendersOrMembers
$AuthorizedSenders=""
if($AcceptMessagesOnlyFrom.Count -gt 0)
{
foreach($item in $AcceptMessagesOnlyFrom)
{
$AuthorizedSenders=$AuthorizedSenders+$item
if($AcceptMessagesOnlyFrom.indexof($item) -lt (($AcceptMessagesOnlyFrom.count)-1))
{
$AuthorizedSenders=$AuthorizedSenders+","
}
}
}
elseif($ExternalSendersAllowed -eq "True")
{
$AuthorizedSenders="Only Senders in Your Organization"
}
else
{
$AuthorizedSenders="Senders inside & Outside of Your Organization"
}
$Manager=""
if($_.ManagedBy.Count -gt 0)
{
foreach($ManageBy in $ManagedBy)
{
$Manager=$Manager+$ManageBy
if($ManagedBy.indexof($ManageBy) -lt (($ManagedBy.count)-1))
{
$Manager=$Manager+","
}
}
}
$Recipient=""
$RecipientHash=@{}
for($KeyIndex = 0; $KeyIndex -lt $RecipientTypeArray.Length; $KeyIndex += 2)
{
$key=$RecipientTypeArray[$KeyIndex]
$Value=$RecipientTypeArray[$KeyIndex+1]
$RecipientHash.Add($key,$Value)
}
$Members=Get-DistributionGroupMember -ResultSize Unlimited -Identity $DisplayName
$MembersCount=($Members.name).Count
#GroupSize Filter
if(([int]$MinGroupMembersCount -ne "") -and ($MembersCount -lt [int]$MinGroupMembersCount))
{
$Print=0
}
#Check for Empty Group
elseif($MembersCount -eq 0)
{
$Member="No Members"
$MemberEmail="-"
$RecipientTypeDetail="-"
Print_Output
}
#Loop through each member in a group
else
{
foreach($Member in $Members)
{
if($IsEmpty.IsPresent)
{
$Print=0
break
}
$RecipientTypeDetail=$Member.RecipientTypeDetails
$MemberEmail=$Member.PrimarySMTPAddress
if($MemberEmail -eq "")
{
$MemberEmail="-"
}
#Get Counts by RecipientTypeDetail
foreach($key in [object[]]$Recipienthash.Keys)
{
if(($RecipientTypeDetail -eq $key) -eq "true")
{
[int]$RecipientHash[$key]+=1
}
}
Print_Output
}
}
#Print Summary report
if($Print -eq 1)
{
#Order RecipientTypeDetail based on count
$Hash=@{}
$Hash=$RecipientHash.GetEnumerator() | Sort-Object -Property value -Descending |foreach{
if([int]$($_.Value) -gt 0 )
{
if($Recipient -ne "")
{ $Recipient+=";"}
$Recipient+=@("$($_.Key) - $($_.Value)")
}
if($Recipient -eq "")
{$Recipient="-"}
}
$Result=@{'DisplayName'=$DisplayName;'PrimarySmtpAddress'=$EmailAddress;'Alias'=$Alias;'GroupType'=$GroupType;'Manager'=$Manager;'GroupMembersCount'=$MembersCount;'MembersCountByType'=$Recipient;'AuthorizedSenders'=$AuthorizedSenders;'ExternalSendersBlocked'=$ExternalSendersAllowed <#;'HiddenFromAddressList'=$_.HiddenFromAddressListsEnabled;
'Description'=$_.Description;'CreationTime'=$_.WhenCreated;'DirSyncEnabled'=$_.IsDirSynced;'JoinGroupWithoutApproval'=$_.MemberJoinRestriction;'LeaveGroupWithoutApproval'=$_.MemberDepartRestriction #>} #Uncomment to print additional attributes in output
$Results= New-Object PSObject -Property $Result
$Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,GroupType,Manager,GroupMembersCount,AuthorizedSenders,ExternalSendersBlocked,MembersCountByType <#,HiddenFromAddressList,Description,CreationTime,DirSyncEnabled,JoinGroupWithoutApproval,LeaveGroupWithoutApproval #> | Export-Csv -Path $ExportSummaryCSV -Notype -Append
}
}
#Print Detailed Output
Function Print_Output
{
if($Print -eq 1)
{
$Result=@{'DisplayName'=$DisplayName;'PrimarySmtpAddress'=$EmailAddress;'Alias'=$Alias;'Members'=$Member;'MemberEmail'=$MemberEmail;'MemberType'=$RecipientTypeDetail}
$Results= New-Object PSObject -Property $Result
$Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,Members,MemberEmail,MemberType | Export-Csv -Path $ExportCSV -Notype -Append
}
}
Function main()
{
#Clean up session
Get-PSSession | Remove-PSSession
#Check for EXO v2 module inatallation
$Module = Get-Module ExchangeOnlineManagement -ListAvailable
if($Module.count -eq 0)
{
Write-Host Exchange Online PowerShell V2 module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Write-host "Installing Exchange Online PowerShell module"
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
Import-Module ExchangeOnlineManagement
}
else
{
Write-Host EXO V2 module is required to connect Exchange Online.Please install module using Install-Module ExchangeOnlineManagement cmdlet.
Exit
}
}
Write-Host Connecting to Exchange Online...
#Storing credential in script for scheduling purpose/ Passing credential as parameter - Authentication using non-MFA account
if(($UserName -ne "") -and ($Password -ne ""))
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword
Connect-ExchangeOnline -Credential $Credential
}
else
{
Connect-ExchangeOnline
}
#Set output file
$ExportCSV=".\DistributionGroup-DetailedMembersReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Detailed report
$ExportSummaryCSV=".\DistributionGroup-SummaryReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Summary report
#Get a list of RecipientTypeDetail
$RecipientTypeArray=Get-Content -Path .\RecipientTypeDetails.txt -ErrorAction Stop
$Result=""
$Results=@()
#Check for input file
if([string]$GroupNamesFile -ne "")
{
#We have an input file, read it into memory
$DG=@()
$DG=Import-Csv -Header "DisplayName" $GroupNamesFile
foreach($item in $DG)
{
Get-DistributionGroup -Identity $item.displayname | Foreach{
$Print=1
Get_Members}
$Count++
}
}
else
{
#Get all distribution group
Get-DistributionGroup -ResultSize Unlimited | Foreach{
$Print=1
Get_Members
$Count++}
}
#Open output file after execution
Write-Host `nScript executed successfully
if((Test-Path -Path $ExportCSV) -eq "True")
{
Write-Host Detailed report available in: $ExportCSV
Write-host Summary report available in: $ExportSummaryCSV
Write-Host `nCheck out """AdminDroid Office 365 Reporting tool""" to get access to 1500+ Office 365 reports.`n -ForegroundColor Green
$Prompt = New-Object -ComObject wscript.shell
$UserInput = $Prompt.popup("Do you want to open output file?",`
0,"Open Output File",4)
If ($UserInput -eq 6)
{
Invoke-Item "$ExportCSV"
Invoke-Item "$ExportSummaryCSV"
}
}
Else
{
Write-Host No DistributionGroup found
}
#Disconnect Exchange Online session
Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
}
. main