-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetDynamicDistributionGroupMembers.ps1
More file actions
283 lines (264 loc) · 9.19 KB
/
GetDynamicDistributionGroupMembers.ps1
File metadata and controls
283 lines (264 loc) · 9.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<#
=============================================================================================
Name: Export Dynamic Distribution Group Members Report
Version: 2.0
Website: o365reports.com
Script Highlights:
~~~~~~~~~~~~~~~~~~
1.The script can be executed with MFA enabled account.
2.Allows you to filter the output based on group size (i.e., Members count).
3.You can choose to either “export members of all Dynamic Distribution Groups” or pass an input file to get members of specific groups alone.
4.Output can be filtered to list Empty group. i.e., DDL without members
5.Exports the result to CSV
6.You can get members count based on Member Type such as user mailbox, Group mailbox, shared mailbox, contact, etc
7.Above all, script stores output in nicely formatted 2 CSV files. One with detailed information and another with summary information.
8.Dynamic Distribution Group – Summary Report : Following are the columns available
a.Group Display Name,
b.Primary SMTP Address,
c.Group Alias,
d.Group Manager,
e.Hidden From Address List,
f.Group Members Count,
g.Members Count by Type
9.Dynamic Distribution Group – Detailed Members Report : Following are the columns available
a.Group Display Name,
b.Primary SMTP Address,
c.Group Alias,
d.Group Manager,
e.Group Members Count,
f.Group Members,
g.Member Email Address
h.Member Type
For detailed script execution: https://o365reports.com/2019/03/23/export-dynamic-distribution-group-members-to-csv/
============================================================================================
#>
#Accept input parameter
Param
(
[Parameter(Mandatory = $false)]
[string]$GroupNamesFile,
[switch]$IsEmpty,
[int]$MinGroupMembersCount,
[string]$UserName,
[string]$Password,
[Switch]$NoMFA
)
#Get group members
Function Get_Members
{
$DisplayName=$_.DisplayName
Write-Progress -Activity "`n Processed Group count: $Count "`n" Getting members of: $DisplayName"
$Alias=$_.Alias
$EmailAddress=$_.PrimarySmtpAddress
$HiddenFromAddressList=$_.HiddenFromAddressListsEnabled
$RecipientFilter=$_.RecipientFilter
$RecipientHash=@{}
for($KeyIndex = 0; $KeyIndex -lt $RecipientTypeArray.Length; $KeyIndex += 2)
{
$key=$RecipientTypeArray[$KeyIndex]
$Value=$RecipientTypeArray[$KeyIndex+1]
$RecipientHash.Add($key,$Value)
}
$Manager=$_.ManagedBy
if($Manager -eq $null)
{
$Manager="-"
}
$Recipient=""
$Members=Get-Recipient -ResultSize unlimited -RecipientPreviewFilter $RecipientFilter
#GroupSize Filter
if(([int]$MinGroupMembersCount -ne "") -and ($Members.count -lt [int]$MinGroupMembersCount))
{
$Print=0
}
#Empty Group Filter
elseif($Members.Count -eq 0)
{
$Member="No Members"
$RecipientTypeDetail="-"
Print_Output
}
#Loop through each member in a group
else
{
foreach($Member in $Members)
{
if($IsEmpty.IsPresent)
{
$Print=0
break
}
#Get Counts by RecipientTypeDetail
$RecipientTypeDetail=$Member.RecipientTypeDetails
$MemberEmail=$Member.PrimarySMTPAddress
foreach($key in [object[]]$Recipienthash.Keys)
{
if(($RecipientTypeDetail -eq $key) -eq "true")
{
[int]$RecipientHash[$key]+=1
}
}
Print_Output
}
}
#Export 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="-"}
}
$Output=@{'DisplayName'=$DisplayName;'PrimarySmtpAddress'=$EmailAddress;'Alias'=$Alias;'Manager'=$Manager;'GroupMembersCount'=$Members.Count;'HiddenFromAddressList'=$HiddenFromAddressList;'MembersCountByType'=$Recipient}
$Outputs= New-Object PSObject -Property $Output
$Outputs | Select-Object DisplayName,PrimarySmtpAddress,Alias,Manager,HiddenFromAddressList,GroupMembersCount,MembersCountByType | Export-Csv -Path $ExportSummaryCSV -Notype -Append
}
}
#Print Detailed Output
Function Print_Output
{
if($Print=1)
{
$Result=@{'DisplayName'=$DisplayName;'PrimarySmtpAddress'=$EmailAddress;'Alias'=$Alias;'Manager'=$Manager;'GroupMembersCount'=$Members.Count; 'Members'=$Member; 'MemberEmail'= $MemberEmail; 'MemberType'=$RecipientTypeDetail}
$Results= New-Object PSObject -Property $Result
$Results | Select-Object DisplayName,PrimarySmtpAddress,Alias,Manager,GroupMembersCount,Members,MemberEmail,MemberType | Export-Csv -Path $ExportCSV -Notype -Append
}
}
Function main()
{
#Get a list of RecipientTypeDetail
$RecipientTypeArray=Get-Content -Path .\RecipientTypeDetails.txt -ErrorAction Stop
#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
}
}
#Check for Azure AD module
$Module = Get-Module MsOnline -ListAvailable
if($Module.count -eq 0)
{
Write-Host MSOnline module is not available -ForegroundColor yellow
$Confirm= Read-Host Are you sure you want to install the module? [Y] Yes [N] No
if($Confirm -match "[yY]")
{
Write-host "Installing MSOnline PowerShell module"
Install-Module MSOnline -Repository PSGallery -AllowClobber -Force
Import-Module MSOnline
}
else
{
Write-Host MSOnline module is required to generate the report.Please install module using Install-Module MSOnline cmdlet.
Exit
}
}
#Authentication using non-MFA
if($NoMFA.IsPresent)
{
#Storing credential in script for scheduling purpose/ Passing credential as parameter
if(($UserName -ne "") -and ($Password -ne ""))
{
$SecuredPassword = ConvertTo-SecureString -AsPlainText $Password -Force
$Credential = New-Object System.Management.Automation.PSCredential $UserName,$SecuredPassword
}
else
{
$Credential=Get-Credential -Credential $null
}
Write-Host "Connecting Azure AD..."
Connect-MsolService -Credential $Credential | Out-Null
Write-Host "Connecting Exchange Online PowerShell..."
Connect-ExchangeOnline -Credential $Credential
}
#Connect to Exchange Online and AzureAD module using MFA
else
{
Write-Host "Connecting Exchange Online PowerShell..."
Connect-ExchangeOnline
Write-Host "Connecting Azure AD..."
Connect-MsolService | Out-Null
}
#Friendly DateTime conversion
if($friendlyTime.IsPresent)
{
If(((Get-Module -Name PowerShellHumanizer -ListAvailable).Count) -eq 0)
{
Write-Host Installing PowerShellHumanizer for Friendly DateTime conversion
Install-Module -Name PowerShellHumanizer
}
}
#Set output file
$ExportCSV=".\DynamicDistributionGroup-DetailedMembersReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Detailed report
$ExportSummaryCSV=".\DynamicDistributionGroup-SummaryReport_$((Get-Date -format yyyy-MMM-dd-ddd` hh-mm` tt).ToString()).csv" #Summary report
$Result=""
$Results=@()
$Count=1
#Check for input file
if ($GroupNamesFile -ne "")
{
#We have an input file, read it into memory
$DDG=@()
$DDG=Import-Csv -Header "DisplayName" $GroupNamesFile
foreach($item in $DDG)
{
Get-DynamicDistributionGroup -Identity $item.displayname | Foreach{
$Print=1
Get_Members}
$Count++
}
}
else
{
#Get all dynamic distribution group
Get-DynamicDistributionGroup | 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 ""
Write-Host " Detailed report available in:" -NoNewline -ForegroundColor Yellow
Write-Host $ExportCSV `n
Write-host " Summary report available in:" -NoNewline -ForegroundColor Yellow
Write-Host $ExportSummaryCSV
Write-Host `n~~ Script prepared by AdminDroid Community ~~`n -ForegroundColor Green
Write-Host "~~ Check out " -NoNewline -ForegroundColor Green; Write-Host "admindroid.com" -ForegroundColor Yellow -NoNewline; Write-Host " to get access to 1800+ Microsoft 365 reports. ~~" -ForegroundColor Green `n`n
$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 DynamicDistributionGroup found
}
#Clean up session
Disconnect-ExchangeOnline -Confirm:$false -InformationAction Ignore -ErrorAction SilentlyContinue
}
. main