-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindEmailAddress.ps1
More file actions
184 lines (154 loc) · 4.53 KB
/
FindEmailAddress.ps1
File metadata and controls
184 lines (154 loc) · 4.53 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
<#
.SYNOPSIS
Find email address in Office 365
.DESCRIPTION
Search trough your mailboxes, distributions groups and Office 365 groups for an Email Addresses or part
of an email address.
.OUTPUTS
List of recipient with mail addresses
.NOTES
Version: 1.2
Author: R. Mens
Creation Date: 14 feb 2022
Purpose/Change: Added exact email address lookup
.EXAMPLE
Seach for a specific email address
.\FindEmailAddress.ps1 -adminUPN admin@contoso.com -emailaddress john@contoso.com
.EXAMPLE
Search for all email addresses with john in it.
.\FindEmailAddress.ps1 -adminUPN admin@contoso.com -emailaddress john
.EXAMPLE
Export results to CSV
.\FindEmailAddress.ps1 -adminUPN admin@contoso.com -emailaddress john | Export-CSV c:\temp\results.csv -NoTypeInformation
#>
param(
[Parameter(
Mandatory = $true,
HelpMessage = "Enter the Exchange Online or Global admin username"
)]
[string]$adminUPN,
[Parameter(
Mandatory = $true,
HelpMessage = "Emailaddress or part of it to find"
)]
[string]$emailAddress,
[Parameter(
Mandatory = $false,
HelpMessage = "Exact match"
)]
[switch]$exact = $false
)
Function ConnectTo-EXO {
<#
.SYNOPSIS
Connects to EXO when no connection exists. Checks for EXO v2 module
#>
process {
# Check if EXO is installed and connect if no connection exists
if ((Get-Module -ListAvailable -Name ExchangeOnlineManagement) -eq $null)
{
Write-Host "Exchange Online PowerShell v2 module is requied, do you want to install it?" -ForegroundColor Yellow
$install = Read-Host Do you want to install module? [Y] Yes [N] No
if($install -match "[yY]")
{
Write-Host "Installing Exchange Online PowerShell v2 module" -ForegroundColor Cyan
Install-Module ExchangeOnlineManagement -Repository PSGallery -AllowClobber -Force
}
else
{
Write-Error "Please install EXO v2 module."
}
}
if ((Get-Module -ListAvailable -Name ExchangeOnlineManagement) -ne $null)
{
# Check if there is a active EXO sessions
$psSessions = Get-PSSession | Select-Object -Property State, Name
If (((@($psSessions) -like '@{State=Opened; Name=ExchangeOnlineInternalSession*').Count -gt 0) -ne $true) {
Connect-ExchangeOnline -UserPrincipalName $adminUPN
}
}
else{
Write-Error "Please install EXO v2 module."
}
}
}
Function Search-Mailboxes {
<#
.SYNOPSIS
Search for email address in the mailboxes
#>
process {
Write-Host "Searching in mailboxes for $emailAddress" -ForegroundColor Cyan
if ($exact) {
Get-EXOMailbox -filter "EmailAddresses -like '$emailAddress'"
}else{
Get-EXOMailbox -filter "EmailAddresses -like '*$emailAddress*'"
}
}
}
Function Search-Distributionlists {
<#
.SYNOPSIS
Search for email address in the distributionlists
#>
process {
Write-Host "Searching in distributionlists for $emailAddress" -ForegroundColor Cyan
if ($exact) {
Get-DistributionGroup -filter "EmailAddresses -like '$emailAddress'"
}else{
Get-DistributionGroup -filter "EmailAddresses -like '*$emailAddress*'"
}
}
}
Function Search-Groups {
<#
.SYNOPSIS
Search for email address in the mailboxes
#>
process {
Write-Host "Searching in groups for $emailAddress" -ForegroundColor Cyan
if ($exact) {
Get-UnifiedGroup -filter "EmailAddresses -like '$emailAddress'"
}else{
Get-UnifiedGroup -filter "EmailAddresses -like '*$emailAddress*'"
}
}
}
Function Search-DynamicDistributionlists {
<#
.SYNOPSIS
Search for email address in the distributionlists
#>
process {
Write-Host "Searching in dynamic distributionlists for $emailAddress" -ForegroundColor Cyan
if ($exact) {
Get-DynamicDistributionGroup -filter "EmailAddresses -like '$emailAddress'"
}else{
Get-DynamicDistributionGroup -filter "EmailAddresses -like '*$emailAddress*'"
}
}
}
Function Find-EmailAddress{
<#
.SYNOPSIS
Get all AD users
#>
process {
$result = @()
$result += Search-Mailboxes
$result += Search-Distributionlists
$result += Search-Groups
$result += Search-DynamicDistributionlists
$result | ForEach {
[pscustomobject]@{
"DisplayName" = $_.DisplayName
"RecipientTypeDetails" = $_.RecipientTypeDetails
"Identity" = $_.identity
"EmailAddresses" = $_.EmailAddresses
}
}
}
}
# Connect to Exchange Online
ConnectTo-EXO
Find-EmailAddress | Sort-Object DisplayName