-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManage-ADMailContacts.ps1
More file actions
189 lines (167 loc) · 6.33 KB
/
Copy pathManage-ADMailContacts.ps1
File metadata and controls
189 lines (167 loc) · 6.33 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
<#
.SYNOPSIS
Active Directory Contact Object and Mail Coexistence Management Tool.
.DESCRIPTION
A menu-driven PowerShell utility designed for Exchange Hybrid environments.
Automates the bulk collection of external user data, provisions local Active
Directory contact objects in a designated OU, waits for directory replication,
and mail-enables the objects with external SMTP routing addresses.
.PARAMETER firstName
The first name of the contact.
.PARAMETER lastName
The last name of the contact.
.PARAMETER email
The target external SMTP address for the contact.
.EXAMPLE
.\Manage-ADHybridContacts.ps1
#>
function Connect-Services {
<#
.SYNOPSIS
Validates and imports required enterprise modules.
#>
Set-ADServerSettings -ViewEntireForest $true
try {
if (-not(Get-Module ActiveDirectory -ListAvailable) -and (Get-Module ExchangeOnlineManagement -ListAvailable)) {
Import-Module -Name ActiveDirectory -Force
}
} catch {
throw $_
}
}
# Initialize required configurations
Connect-Services
Write-Host "`n###### Create Hybrid Contact Object Utilities ######" -ForegroundColor Green
function Get-UserInfo {
<#
.SYNOPSIS
Interactively collects and validates contact metadata from the operator.
#>
param (
[string]$firstName,
[string]$lastName,
[string]$fullName,
[string]$email,
[string]$company,
[string]$title,
[string]$phoneNumber
)
$contacts = @()
do {
$firstName = (Read-Host "First Name").Trim()
$lastName = (Read-Host "Last Name").Trim()
$fullName = $firstName + " " + $lastName
$email = (Read-Host "Email Address").Trim()
$company = (Read-Host "Company").Trim()
$title = (Read-Host "Title").Trim()
# Validate phone number format via RegEx loop
do {
$phoneNumber = Read-Host "Phone Number (123-456-7890 or leave blank)"
$phoneNumberPattern = '^\d{3}-\d{3}-\d{4}$'
if ($phoneNumber -eq "" -or $phoneNumber -match $phoneNumberPattern) {
break
} else {
Write-Host "Invalid phone number format. Please use 123-456-7890." -ForegroundColor Red
}
} while ($true)
$contacts += [PSCustomObject]@{
FirstName = $firstName
LastName = $lastName
FullName = $fullName
Email = $email
Company = $company
Title = $title
PhoneNumber = $phoneNumber
Date = Get-Date -Format "MM/dd/yyyy hh:mm:ss tt"
}
$more = Read-Host "`nDo you want to add another contact? (Y/N)"
Write-Host "---------------------------------------------"
} while ($more -eq 'Y' -or $more -eq 'y')
return $contacts
}
function Add-ContactObjectName {
<#
.SYNOPSIS
Provisions the base contact object within Active Directory.
#>
param (
[string]$fullName
)
try {
# Genericized Target OU Path for Public Portfolio Safety
$TargetOU = "OU=Contacts,OU=HybridObjects,OU=Exchange,DC=yourdomain,DC=com"
New-ADObject -Name "$fullName" -Type "contact" -Path $TargetOU
Write-Output "Contact object '$fullName' created successfully."
} catch {
if ($_ -match '\*Error: An attempt was made to add an object') {
Write-Host "Failed to create contact object '$fullName' because this name is already in use." -ForegroundColor Red
} else {
Write-Error "Failed to create contact object '$fullName'. Error: $($_.Exception.Message)"
}
}
}
function Add-ContactObjectUserInfo {
<#
.SYNOPSIS
Mail-enables the AD contact and maps modern routing attributes.
#>
param (
[string]$firstName,
[string]$lastName,
[string]$fullName,
[string]$email,
[string]$company,
[string]$title,
[string]$phoneNumber
)
try {
# Establish mail coexistence attributes
Enable-MailContact -Identity $fullName -ExternalEmailAddress $email
Set-Contact -Identity "$fullName" -FirstName "$firstName" -LastName "$lastName" -Company "$company" -Title "$title" -Phone "$phoneNumber"
Write-Output "`nMail contact for '$fullName ($email)' enabled successfully."
} catch {
Write-Error "Failed to enable mail contact for '$fullName'. Error: $($_.Exception.Message)"
}
}
function Add-Contact {
<#
.SYNOPSIS
Orchestrates batch processing and accounts for replication delays.
#>
$userContacts = Get-UserInfo
# Optional localized transaction logging
$LogPath = "$env:USERPROFILE\Desktop\ContactObject_Log.csv"
$userContacts | Export-Csv -Path $LogPath -Append -NoTypeInformation
# Phase 1: Create base objects in Active Directory
foreach ($contact in $userContacts) {
Add-ContactObjectName -fullName $contact.FullName
}
# Phase 2: Active Directory Replication Wait Interval
Write-Output "Waiting 3 minutes to ensure multi-DC replication settles before running Exchange lifecycle cmdlets..."
Start-Sleep -Seconds 150
# Phase 3: Apply Exchange attributes and address routing
foreach ($contact in $userContacts) {
Add-ContactObjectUserInfo -fullName $contact.FullName -FirstName $contact.FirstName -lastName $contact.LastName -email $contact.Email -company $contact.Company -title $contact.Title -phoneNumber $contact.PhoneNumber
}
}
function Show-MainMenu {
Write-Host "`n====================="
Write-Host "1. Create Contact"
Write-Host "2. Delete Contact [Placeholder]"
Write-Host "3. Update Contact [Placeholder]"
Write-Host "4. Exit"
Write-Host "====================="
}
# Main execution frame loop
do {
Show-MainMenu
$choice = Read-Host "Please select an Option"
switch ($choice) {
1 { Add-Contact }
2 { Write-Host "Delete module pending implementation." -ForegroundColor Yellow }
3 { Write-Host "Update module pending implementation." -ForegroundColor Yellow }
4 { break }
Default { Write-Host "Invalid option, please select again." -ForegroundColor Red }
}
$continueMainMenu = Read-Host "Return to Main Menu? (y/n)"
} while ($continueMainMenu -eq 'y' -or $continueMainMenu -eq 'Y')