-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-SmtpAddress.ps1
More file actions
53 lines (44 loc) · 1.98 KB
/
Copy pathGet-SmtpAddress.ps1
File metadata and controls
53 lines (44 loc) · 1.98 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
<#
.SYNOPSIS
Searches for mailboxes and distribution groups associated with a specified SMTP address.
.DESCRIPTION
The Get-SmtpAddress function searches for mailboxes and distribution groups associated with a specified SMTP address.
It retrieves mailboxes and distribution groups whose email addresses or primary SMTP addresses match the provided SMTP address.
.PARAMETER smtpAddress
Specifies the SMTP address to search for within mailboxes and distribution groups.
.EXAMPLE
Get-SmtpAddress -smtpAddress 'user@example.com'
Searches for mailboxes and distribution groups associated with the SMTP address 'user@example.com'.
#>
function Get-SmtpAddress {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0)]
[string]$smtpAddress
)
# Get the mailbox with the specified SMTP address
$mailbox = Get-Mailbox | Where-Object { ($_.EmailAddresses -eq "$smtpAddress") -or ($_.PrimarySmtpAddress -eq "$smtpAddress") }
# Get all distribution groups and filter them by SMTP address
$distributionGroups = Get-DistributionGroup -ResultSize Unlimited
$matchingDistributionGroups = $distributionGroups | Where-Object {
$_.PrimarySmtpAddress -eq "$smtpAddress"
}
if ($mailbox -ne $null) {
Write-Host "Mailbox with SMTP address '$smtpAddress' found:"
$mailbox | Select Name, Alias
} elseif ($matchingDistributionGroups) {
$groupCount = @($matchingDistributionGroups).Count
if ($groupCount -gt 0) {
Write-Host "Distribution group with SMTP address '$smtpAddress' found:"
$matchingDistributionGroups | Select Name, DisplayName
}
} else {
Write-Host "No mailbox or distribution group found with SMTP address '$smtpAddress'."
}
}
# Prompt the user for SMTP address if not provided
if (-not $smtpAddress) {
$smtpAddress = Read-Host -Prompt "Enter SMTP address"
}
# Call the function with the provided SMTP address
Get-SmtpAddress -smtpAddress $smtpAddress