-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export-DLMB.psm1
84 lines (79 loc) · 3.45 KB
/
Export-DLMB.psm1
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
function Export-DLMB {
<#
.SYNOPSIS
This CMDLET Exports Members in a Distribution List and Mail Enabled Security Group
.DESCRIPTION
Exports Members in a Distribution List and Mail Enabled Security Group
To a CSV File called (DL&MESGMembers.csv or to a specified CSV File and File Path)
.EXAMPLE
Export-DLMB "C:\Users\Public"
=============================
Export-DLMB -LocationPath "C:\Users\Public"
===========================================
Export-DLMB -LocationPath "C:\Users\Public" -FileName "myfile"
==============================================================
Export-DLMB -LP "C:\Users\Public" -FN "myfile"
.INPUTS
None
.OUTPUTS
CSV file named DL&MESGMembers.csv Or the specified file stated in Parameter -FileName
CSV contains DL&MESGName, NameOfMembers, PrimarySmtpAddress, RecipientTypeDetails
.NOTES
For this to run you will need to connect to Exchange Online via PowerShell
If no value is entered for parameter FileName a default file "DL&MESGMembers.csv" is created
#>
[CmdletBinding()]
param (
# Specifies a path to one or more locations.
[Parameter(Mandatory = $true,
Position = 0,
ParameterSetName = "Location",
ValueFromPipeline = $false,
ValueFromPipelineByPropertyName = $false,
HelpMessage = "Please enter a valid Location")]
[Alias("LP")]
[ValidateNotNullOrEmpty()]
[string]
$LocationPath,
# Specifies a path to one or more locations.
[Parameter(Mandatory = $false,
Position = 1,
ValueFromPipeline = $false,
ValueFromPipelineByPropertyName = $false,
HelpMessage = "Please enter the File Name")]
[Alias("FN")]
[ValidateNotNullOrEmpty()]
[string]
$FileName = "DL&MESGMembers"
)
# begin {
# }
process {
#Get Distribution Groups/Mail Enabled Security Groups
$DlGroups = Get-DistributionGroup -ResultSize Unlimited | Select-Object PrimarySmtpAddress
#Creating an array
$FinalArray = @()
#Loop through the List of DL/MESG
Foreach ($DlGroup in $DlGroups) {
$Upn = $DlGroup.PrimarySmtpAddress.ToString()
#Get Distribution Groups Members/Mail Enabled Security Groups Members
$Members = Get-DistributionGroupMember -ResultSize Unlimited -Identity $Upn | Select-Object Name, PrimarySmtpAddress, RecipientTypeDetails
#Create a HashTable for the values
$FinalArray += [PSCustomObject][Ordered]@{
"DL&MESGName" = $Upn
"NameOfMembers" = ($Members.Name | Out-String).Trim()
"PrimarySmtpAddress" = ($Members.PrimarySmtpAddress | Out-String).Trim()
"RecipientTypeDetails" = ($Members.RecipientTypeDetails | Out-String).Trim()
}
}
}
end {
#_Path parameter for Export-CSV
[string]$Script:FinalName = ($LocationPath + "\" + $FileName + ".csv")
#Export all members to CSV called variable $FinalName
$FinalArray | Export-Csv -Path $FinalName -Delimiter "," -NoTypeInformation
#Writes Output to Screen
Write-Output $FinalArray
Write-Host ("`n`r Kindly Check this Location " + $FinalName) -BackgroundColor Green -ForegroundColor DarkBlue
}
}