-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEnable-MailRoutingContact.ps1
246 lines (183 loc) · 8.05 KB
/
Enable-MailRoutingContact.ps1
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
<#
.SYNOPSIS
This function mail enables mail routing contact for hybrid mail flow.
.DESCRIPTION
This function mail enables mail routing contact for hybrid mail flow.
.PARAMETER GlobalCatalogServer
The global catalog to make the query against.
.PARAMETER routingContactConfig
The original DN of the object.
.OUTPUTS
None
.EXAMPLE
enable-mailRoutingContact -globalCatalogServer GC -routingContactConfig contactConfiguration.
#>
Function Enable-MailRoutingContact
{
[cmdletbinding()]
Param
(
[Parameter(Mandatory = $true)]
[string]$globalCatalogServer,
[Parameter(Mandatory = $true)]
$routingContactConfig,
[Parameter(Mandatory = $true)]
$routingXMLFile
)
#Output all parameters bound or unbound and their associated values.
write-functionParameters -keyArray $MyInvocation.MyCommand.Parameters.Keys -parameterArray $PSBoundParameters -variableArray (Get-Variable -Scope Local -ErrorAction Ignore)
$isTestError="No"
#Declare function variables.
$functionGroup=$NULL
$functionOutputGroup=$NULL
$functionRemoteRoutingAddress=$NULL
#Start function processing.
Out-LogFile -string "********************************************************************************"
Out-LogFile -string "BEGIN enable-mailRoutingContact"
Out-LogFile -string "********************************************************************************"
#Log the parameters and variables for the function.
#Updated the mail contact so that it has full mail attributes.
try{
out-logfile -string "Updating the mail contact..."
update-recipient -identity $routingContactConfig.distinguishedName -domainController $globalCatalogServer -errorAction STOP
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Obtain the mail contact configuration into a temporary variable now that it's a full recipient.
try{
out-logfile -string "Gathering the mail contact configuration..."
$functionGroup=get-mailContact -identity $routingContactConfig.distinguishedName -domainController $globalCatalogServer -errorAction STOP
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Write the updated recipient out.
try
{
out-xmlFile -itemToExport $functionGroup -itemNameTOExport (($routingXMLFile)+"-UpdateRecipient")
}
catch {
out-logfile -string "Unable to output to XML file."
out-logfile -string $_ -isError:$TRUE
}
#The mail contact may need upgrade to the "latest version."
try{
out-logfile -string "Forcing upgrade to contact - necessary in order to provision."
set-mailcontact -identity $functionGroup.distinguishedName -domainController $globalCatalogServer -ForceUpgrade
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Obtain the mail contact configuration into a temporary variable now that it's a full recipient.
try{
out-logfile -string "Gathering the mail contact configuration..."
$functionOutputGroup=get-mailContact -identity $routingContactConfig.distinguishedName -domainController $globalCatalogServer -errorAction STOP
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Write the updated recipient out.
try
{
out-xmlFile -itemToExport $functionOutputGroup -itemNameTOExport (($routingXMLFile)+"-UpgradedRecipient")
}
catch {
out-logfile -string "Unable to output to XML file."
out-logfile -string $_ -isError:$TRUE
}
#The mail contact may need upgrade to the "latest version."
try{
out-logfile -string "Setting email address policy enabled to $FALSE - stop further automatic email addressing."
set-mailcontact -identity $functionGroup.distinguishedName -EmailAddressPolicyEnabled:$FALSE -domainController $globalCatalogServer -forceUpgrade -confirm:$FALSE
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Obtain the mail contact configuration into a temporary variable now that it's a full recipient.
try{
out-logfile -string "Gathering the mail contact configuration..."
$functionOutputGroup=get-mailContact -identity $routingContactConfig.distinguishedName -domainController $globalCatalogServer -errorAction STOP
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Write the updated recipient out.
try
{
out-xmlFile -itemToExport $functionOutputGroup -itemNameTOExport (($routingXMLFile)+"-DisableEmailPolicy")
}
catch {
out-logfile -string "Unable to output to XML file."
out-logfile -string $_ -isError:$TRUE
}
try{
out-logfile -string "Removing the remote routing address..."
set-mailContact -identity $routingContactConfig.distinguishedName -primarySMTPAddress $routingContactConfig.mail -domainController $globalCatalogServer -forceUpgrade -confirm:$FALSE -errorAction STOP
}
catch{
out-logfile -string $_ -isError:$TRUE
}
#Obtain the mail contact configuration into a temporary variable now that it's a full recipient.
try{
out-logfile -string "Gathering the mail contact configuration..."
$functionOutputGroup=get-mailContact -identity $routingContactConfig.distinguishedName -domainController $globalCatalogServer -errorAction STOP
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Write the updated recipient out.
try
{
out-xmlFile -itemToExport $functionOutputGroup -itemNameTOExport (($routingXMLFile)+"-MailUpdate")
}
catch {
out-logfile -string "Unable to output to XML file."
out-logfile -string $_ -isError:$TRUE
}
#Removee the target address from the list of proxy addresses so it does not collide with the migrated group.
try{
out-logfile -string "Removing the remote routing address..."
set-mailContact -identity $routingContactConfig.distinguishedName -emailaddresses @{remove=$routingContactConfig.targetAddress} -domainController $globalCatalogServer -forceUpgrade -confirm:$FALSE -errorAction STOP
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Obtain the mail contact configuration into a temporary variable now that it's a full recipient.
try{
out-logfile -string "Gathering the mail contact configuration..."
$functionOutputGroup=get-mailContact -identity $routingContactConfig.distinguishedName -domainController $globalCatalogServer -errorAction STOP
}
catch{
out-logfile -string $_
$isTestError="Yes"
return $isTestError
}
#Write the updated recipient out.
try
{
out-xmlFile -itemToExport $functionOutputGroup -itemNameTOExport (($routingXMLFile)+"-RemoteTargetAddress")
}
catch {
out-logfile -string "Unable to output to XML file."
out-logfile -string $_ -isError:$TRUE
}
Out-LogFile -string "END enable-mailRoutingContact"
Out-LogFile -string "********************************************************************************"
}