Repository navigation
Expand file tree
/
Copy pathCopy-ReceiveConnector.ps1
More file actions
646 lines (513 loc) · 22.9 KB
/
Copy pathCopy-ReceiveConnector.ps1
File metadata and controls
646 lines (513 loc) · 22.9 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<#
.SYNOPSIS
Copy a selected receive connector and its configuration and permissions to other Exchange Servers
Thomas Stensitzki
THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
Version 1.9, 2026-10-27
Please submit ideas, comments, and suggestions using GitHub.
.LINK
https://scripts.granikos.eu
.DESCRIPTION
This script copies a receive connector from a source Exchange Server to a single target Exchange server or to all Exchange servers.
Configured permissions are copied as well, if required.
All values read from the source connector are converted explicitly to strings before they are handed over to
New-ReceiveConnector, Set-ReceiveConnector, and Add-ADPermission. This is required because the Exchange Management Shell
uses PowerShell remoting and returns deserialized objects. Since PowerShell serialization payload signing is enabled by default,
Exchange no longer converts deserialized Exchange types (e.g. IPRange) back automatically.
.NOTES
Requirements
- Windows Server 2016, 2019, 2022, or 2025
- Exchange Server 2016, Exchange Server 2019, or Exchange Server Subscription Edition (SE)
- Exchange Management Shell
Revision History
--------------------------------------------------------------------------------
1.0 Initial community release
1.1 Domain Controller parameter added, permissions group copy added
1.2 Move to FrontendTransport added, optional permission copy added, reset bindings added
1.3 Update receive connector, if receive connector exists
1.4 Fix to handle connector updates properly
1.41 Minor fixes and update for Exchange 2016
1.5 Issue #2 fixed
1.6 Issue #3 fixed
1.6.1 Minor fixes and tested with Exchange Server 2019
1.7 UpdateExistingConnector added to bypass update confirmation for automation
1.8 Check for Exchange Server 2019 and PowerShell Serialization Signing
1.9 Issue #7 fixed (RemoteIPRanges and other values cannot be converted)
All source values are converted explicitly, independent of serialization payload signing
Source connector is read once, DomainController is used consistently
Existing non-inherited AD permissions on the target connector are skipped
Warning for server specific network bindings
Additional connector properties copied, if supported by the target cmdlet
-WhatIf support, SecondsToWait parameter added
Unused ActiveDirectory module dependency removed
.PARAMETER SourceServer
Source Exchange server hosting the receive connector to copy.
.PARAMETER ConnectorName
Name of the receive connector to copy.
.PARAMETER TargetServer
Target Exchange server to copy the selected receive connector to.
.PARAMETER DomainController
The name of a domain controller. All read and write operations use this domain controller.
.PARAMETER CopyToAllOther
Switch to copy to all other Exchange servers with the Mailbox role.
.PARAMETER CopyPermissions
Copy non inherited source receive AD permissions to target receive connector. Inherited permissions will not be copied.
Permissions that already exist on the target connector are skipped.
.PARAMETER MoveToFrontend
Change source connector transport role to FrontendTransport. This is required when you copy a receive connector from Exchange 2010 to Exchange 2013+.
.PARAMETER ResetBindings
Do not copy bindings but reset receive connector network bindings to 0.0.0.0:25.
.PARAMETER UpdateExistingConnector
Switch to force updating an existing receive connector on the target server without confirmation prompt.
.PARAMETER ViewEntireForest
Set scope to view entire Active Directory forest.
.PARAMETER SecondsToWait
Seconds to wait for Active Directory before AD permissions or permission groups are updated on a newly created connector. Default: 60
.EXAMPLE
Copy receive connector MYRECEIVECONNECTOR from server MBX01 to server MBX2
.\Copy-ReceiveConnector.ps1 -SourceServer MBX01 -ConnectorName MYRECEIVECONNECTOR -TargetServer MBX2 -DomainController MYDC1.mcsmemail.de
.EXAMPLE
Copy receive connector MYRECEIVECONNECTOR from server MBX01 to all other Exchange servers
.\Copy-ReceiveConnector.ps1 -SourceServer MBX01 -ConnectorName MYRECEIVECONNECTOR -CopyToAllOther -DomainController MYDC1.mcsmemail.de
.EXAMPLE
Copy receive connector MYRECEIVECONNECTOR from Exchange 2010 server MBX2010 to Exchange 2016 server MBX01, make it a FrontEnd-Connector, and reset network bindings
.\Copy-ReceiveConnector.ps1 -SourceServer MBX2010 -ConnectorName MYRECEIVECONNECTOR -TargetServer MBX01 -MoveToFrontend -ResetBindings -DomainController MYDC1.mcsmemail.de
.EXAMPLE
Copy receive connector MYRECEIVECONNECTOR from server MBX01 to all other Exchange servers, including permissions, and update the connector if it already exists
.\Copy-ReceiveConnector.ps1 -SourceServer MBX01 -ConnectorName MYRECEIVECONNECTOR -CopyToAllOther -DomainController MYDC1.mcsmemail.de -CopyPermissions -UpdateExistingConnector
.EXAMPLE
Show what would happen without changing anything
.\Copy-ReceiveConnector.ps1 -SourceServer MBX01 -ConnectorName MYRECEIVECONNECTOR -TargetServer MBX2 -DomainController MYDC1.mcsmemail.de -CopyPermissions -WhatIf
#>
[CmdletBinding(SupportsShouldProcess)]
param(
[parameter(Mandatory,HelpMessage='Source Exchange server to copy from')]
[string]$SourceServer,
[parameter(Mandatory,HelpMessage='Name of the source receive connector')]
[string]$ConnectorName,
[string]$TargetServer = '',
[parameter(Mandatory,HelpMessage='Domain Controller name')]
[string]$DomainController,
[switch]$CopyToAllOther,
[switch]$CopyPermissions,
[switch]$MoveToFrontend,
[switch]$ResetBindings,
[switch]$UpdateExistingConnector,
[switch]$ViewEntireForest,
[ValidateRange(0,600)]
[int]$SecondsToWait = 60
)
### FUNCTIONS -----------------------------
function Request-Choice {
[CmdletBinding()]
param(
[string]$Caption = 'Really?'
)
$choices = [System.Management.Automation.Host.ChoiceDescription[]]@('&Yes','&No')
[int]$defaultChoice = 1
$choiceReturn = $Host.UI.PromptForChoice($Caption, '', $choices, $defaultChoice)
return $choiceReturn
}
function ConvertTo-StringArray {
# Converts single values, collections, and deserialized Exchange objects to a plain string array
[CmdletBinding()]
param(
$Value
)
$result = New-Object -TypeName System.Collections.Generic.List[string]
foreach($item in @($Value)) {
if($null -ne $item) {
$itemString = ([string]$item).Trim()
if($itemString -ne '') {
$result.Add($itemString)
}
}
}
return ,$result.ToArray()
}
function ConvertTo-Boolean {
[CmdletBinding()]
param(
$Value
)
if($null -eq $Value) {
return $false
}
return [System.Convert]::ToBoolean([string]$Value)
}
function ConvertTo-SizeString {
# Converts a (deserialized) ByteQuantifiedSize like '36 MB (37,748,736 bytes)' to '37748736B'
[CmdletBinding()]
param(
$Value
)
if($null -eq $Value) {
return $null
}
$sizeString = ([string]$Value).Trim()
if($sizeString -match '\(([\d\.,\s]+)\s*bytes\)') {
return ('{0}B' -f ($Matches[1] -replace '[^\d]',''))
}
return $sizeString
}
function ConvertTo-FlagString {
# Converts a (deserialized) flags enum like PermissionGroups or AuthMechanism to a comma separated string
[CmdletBinding()]
param(
$Value,
[string[]]$Exclude = @()
)
$flags = @((ConvertTo-StringArray -Value $Value) -join ',' -split ',' |
ForEach-Object -Process { $_.Trim() } |
Where-Object -FilterScript { ($_ -ne '') -and ($Exclude -notcontains $_) })
return ($flags -join ', ')
}
function Test-ServerSpecificBinding {
# Returns $true, if a binding uses a dedicated IP address instead of all IPv4/IPv6 addresses
[CmdletBinding()]
param(
[string]$Binding
)
$address = $Binding.Substring(0, $Binding.LastIndexOf(':'))
return (@('0.0.0.0','[::]','::') -notcontains $address)
}
function Get-ReceiveConnectorParameter {
# Builds the splatting hashtable for New-ReceiveConnector or Set-ReceiveConnector from the source connector
[CmdletBinding()]
param(
[Parameter(Mandatory)]
$Connector,
[Parameter(Mandatory)]
[string]$TargetFqdn,
[Parameter(Mandatory)]
[ValidateSet('New-ReceiveConnector','Set-ReceiveConnector')]
[string]$CommandName,
[switch]$IsUpdate
)
$params = @{}
# Multi-valued properties, the cause of issue #7
$params.RemoteIPRanges = ConvertTo-StringArray -Value $Connector.RemoteIPRanges
# Flags. The Custom permission group cannot be set and is represented by non-inherited AD permissions
$permissionGroups = ConvertTo-FlagString -Value $Connector.PermissionGroups -Exclude 'Custom'
if($permissionGroups -eq '') {
$permissionGroups = 'None'
}
$params.PermissionGroups = $permissionGroups
$params.AuthMechanism = ConvertTo-FlagString -Value $Connector.AuthMechanism
$params.Fqdn = $TargetFqdn
# Boolean properties
$booleanProperties = @(
'AdvertiseClientSettings'
'BinaryMimeEnabled'
'ChunkingEnabled'
'DeliveryStatusNotificationEnabled'
'DomainSecureEnabled'
'EightBitMimeEnabled'
'EnableAuthGSSAPI'
'Enabled'
'EnhancedStatusCodesEnabled'
'LongAddressesEnabled'
'OrarEnabled'
'PipeliningEnabled'
'RequireEHLODomain'
'RequireTLS'
'SuppressXAnonymousTls'
)
foreach($property in $booleanProperties) {
if($null -ne $Connector.$property) {
$params.$property = ConvertTo-Boolean -Value $Connector.$property
}
}
# Properties that are handed over as string (TimeSpan, Unlimited<int>, int, enums, domain names)
$stringProperties = @(
'ConnectionInactivityTimeout'
'ConnectionTimeout'
'DefaultDomain'
'ExtendedProtectionPolicy'
'MaxAcknowledgementDelay'
'MaxHopCount'
'MaxInboundConnection'
'MaxInboundConnectionPercentagePerSource'
'MaxInboundConnectionPerSource'
'MaxLocalHopCount'
'MaxLogonFailures'
'MaxProtocolErrors'
'MaxRecipientsPerMessage'
'MessageRateLimit'
'MessageRateSource'
'ProtocolLoggingLevel'
'ServiceDiscoveryFqdn'
'SizeEnabled'
'TarpitInterval'
)
foreach($property in $stringProperties) {
$propertyValue = [string]$Connector.$property
if($propertyValue -ne '') {
$params.$property = $propertyValue
}
}
# ByteQuantifiedSize properties
foreach($property in @('MaxHeaderSize','MaxMessageSize')) {
$propertyValue = ConvertTo-SizeString -Value $Connector.$property
if(($null -ne $propertyValue) -and ($propertyValue -ne '')) {
$params.$property = $propertyValue
}
}
# Properties that can be empty. Empty values are used to clear the property when updating an existing connector
$banner = [string]$Connector.Banner
$comment = [string]$Connector.Comment
$tlsCertificateName = [string]$Connector.TlsCertificateName
$tlsDomainCapabilities = ConvertTo-StringArray -Value $Connector.TlsDomainCapabilities
if($banner -ne '') { $params.Banner = $banner } elseif($IsUpdate) { $params.Banner = $null }
if($comment -ne '') { $params.Comment = $comment } elseif($IsUpdate) { $params.Comment = $null }
if($tlsCertificateName -ne '') { $params.TlsCertificateName = $tlsCertificateName } elseif($IsUpdate) { $params.TlsCertificateName = $null }
if($tlsDomainCapabilities.Count -gt 0) { $params.TlsDomainCapabilities = $tlsDomainCapabilities } elseif($IsUpdate) { $params.TlsDomainCapabilities = $null }
# Remove parameters the cmdlet of the current Exchange version does not support
$availableParameters = (Get-Command -Name $CommandName).Parameters.Keys
foreach($key in @($params.Keys)) {
if($availableParameters -notcontains $key) {
Write-Verbose -Message ('{0} does not support parameter {1}. Skipping.' -f $CommandName, $key)
$params.Remove($key)
}
}
return $params
}
function Get-AceKey {
# Builds a comparable key for an AD permission entry
[CmdletBinding()]
param(
$Ace
)
return ('{0}|{1}|{2}|{3}|{4}' -f ([string]$Ace.User).ToLower(),
(ConvertTo-Boolean -Value $Ace.Deny),
(((ConvertTo-StringArray -Value $Ace.AccessRights) | Sort-Object) -join ','),
(((ConvertTo-StringArray -Value $Ace.ExtendedRights) | Sort-Object) -join ','),
(((ConvertTo-StringArray -Value $Ace.Properties) | Sort-Object) -join ','))
}
function Copy-ConnectorPermission {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
$SourceConnector,
[Parameter(Mandatory)]
[string]$TargetIdentity
)
# fetch non inherited permissions from source connector
$sourcePermissions = @(Get-ADPermission -Identity ([string]$SourceConnector.DistinguishedName) -DomainController $DomainController |
Where-Object -FilterScript { -not (ConvertTo-Boolean -Value $_.IsInherited) })
if($sourcePermissions.Count -eq 0) {
Write-Verbose -Message 'Source connector has no non-inherited AD permissions'
return
}
$targetRC = Get-ReceiveConnector -Identity $TargetIdentity -DomainController $DomainController -ErrorAction SilentlyContinue
if($null -eq $targetRC) {
# Happens with -WhatIf, when the target connector has not been created
Write-Verbose -Message ('Target connector {0} not found. {1} AD permissions would be copied.' -f $TargetIdentity, $sourcePermissions.Count)
return
}
$targetDN = [string]$targetRC.DistinguishedName
# Fetch existing permissions on target to avoid duplicate ACE errors
$existingAceKeys = @(Get-ADPermission -Identity $targetDN -DomainController $DomainController |
Where-Object -FilterScript { -not (ConvertTo-Boolean -Value $_.IsInherited) } |
ForEach-Object -Process { Get-AceKey -Ace $_ })
Write-Verbose -Message 'Adding AD permissions'
$added = 0
$skipped = 0
foreach($ace in $sourcePermissions) {
$user = [string]$ace.User
if($user -eq '') {
continue
}
if($existingAceKeys -contains (Get-AceKey -Ace $ace)) {
Write-Verbose -Message ('Permission for {0} already exists on target. Skipping.' -f $user)
$skipped++
continue
}
$aceParams = @{
Identity = $targetDN
User = $user
Deny = (ConvertTo-Boolean -Value $ace.Deny)
DomainController = $DomainController
}
$accessRights = ConvertTo-StringArray -Value $ace.AccessRights
$extendedRights = ConvertTo-StringArray -Value $ace.ExtendedRights
$adProperties = ConvertTo-StringArray -Value $ace.Properties
$inheritanceType = [string]$ace.InheritanceType
if($accessRights.Count -gt 0) { $aceParams.AccessRights = $accessRights }
if($extendedRights.Count -gt 0) { $aceParams.ExtendedRights = $extendedRights }
if($adProperties.Count -gt 0) { $aceParams.Properties = $adProperties }
if($inheritanceType -ne '') { $aceParams.InheritanceType = $inheritanceType }
if($PSCmdlet.ShouldProcess($TargetIdentity, ('Add AD permission for {0} ({1})' -f $user, ($accessRights + $extendedRights -join ', ')))) {
try {
$null = Add-ADPermission @aceParams -ErrorAction Stop
$added++
}
catch {
Write-Warning -Message ('Failed to add AD permission for {0} on {1}: {2}' -f $user, $TargetIdentity, $_.Exception.Message)
}
}
}
Write-Host ('AD permissions on {0}: {1} added, {2} already existing' -f $TargetIdentity, $added, $skipped)
}
function Wait-ForDirectory {
[CmdletBinding()]
param()
if($SecondsToWait -gt 0) {
Write-Host ('Wait {0} seconds for domain controller to update' -f $SecondsToWait)
Start-Sleep -Seconds $SecondsToWait
}
}
function Copy-ToServer {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
$SourceConnector,
[Parameter(Mandatory)]
$SourceServerObject,
[Parameter(Mandatory)]
[string]$TargetServerName
)
$targetServerObject = Get-ExchangeServer -Identity $TargetServerName -DomainController $DomainController -ErrorAction SilentlyContinue
if($null -eq $targetServerObject) {
Write-Warning -Message ('Target server {0} cannot be found.' -f $TargetServerName)
return
}
$targetName = [string]$targetServerObject.Name
if($targetName -eq [string]$SourceServerObject.Name) {
Write-Warning -Message 'Source and target server are identical. Skipping.'
return
}
$targetIdentity = '{0}\{1}' -f $targetName, $ConnectorName
Write-Host ('Working on [{0}] and receive connector [{1}]' -f $targetName.ToUpper(), $ConnectorName)
# Replace the source server FQDN with the target server FQDN
$targetFqdn = [string]$SourceConnector.Fqdn
$sourceServerFqdn = [string]$SourceServerObject.Fqdn
if(($targetFqdn -eq $sourceServerFqdn) -or ($targetFqdn -like ('{0}.*' -f $SourceServerObject.Name))) {
Write-Verbose -Message 'Source connector uses server Fqdn. Changing to target server Fqdn.'
$targetFqdn = ([string]$targetServerObject.Fqdn).ToLower()
}
$targetRC = Get-ReceiveConnector -Identity $targetIdentity -DomainController $DomainController -ErrorAction SilentlyContinue
if($null -eq $targetRC) {
# create new receive connector
$params = Get-ReceiveConnectorParameter -Connector $SourceConnector -TargetFqdn $targetFqdn -CommandName 'New-ReceiveConnector'
$params.Name = [string]$SourceConnector.Name
$params.Server = $targetName
$params.DomainController = $DomainController
if($MoveToFrontend) {
# Move receive connector to FrontEnd Transport
$params.TransportRole = 'FrontendTransport'
}
else {
$params.TransportRole = [string]$SourceConnector.TransportRole
}
if($ResetBindings) {
# Reset network bindings to listen on all adapters using port 25
$params.Bindings = @('0.0.0.0:25')
}
else {
$params.Bindings = ConvertTo-StringArray -Value $SourceConnector.Bindings
$serverSpecificBindings = @($params.Bindings | Where-Object -FilterScript { Test-ServerSpecificBinding -Binding $_ })
if($serverSpecificBindings.Count -gt 0) {
Write-Warning -Message ('Source connector uses server specific bindings ({0}). Use -ResetBindings or adjust the bindings on {1} afterwards.' -f ($serverSpecificBindings -join ', '), $targetName)
}
}
# ExternalAuthoritative requires ExchangeServers permission group during creation
$restorePermissionGroups = $null
if((($params.AuthMechanism -split ',').Trim() -contains 'ExternalAuthoritative') -and ($params.PermissionGroups -ne 'ExchangeServers')) {
Write-Verbose -Message 'Connector is set to ExternalAuthoritative and needs special treatment.'
$restorePermissionGroups = $params.PermissionGroups
$params.PermissionGroups = 'ExchangeServers'
}
if($PSCmdlet.ShouldProcess($targetIdentity, 'Create receive connector')) {
try {
$null = New-ReceiveConnector @params -ErrorAction Stop
}
catch {
Write-Warning -Message ('Failed to create receive connector {0}: {1}' -f $targetIdentity, $_.Exception.Message)
return
}
if($null -ne $restorePermissionGroups) {
Wait-ForDirectory
Write-Verbose -Message ('Updating PermissionGroups for {0}' -f $targetIdentity)
Set-ReceiveConnector -Identity $targetIdentity -PermissionGroups $restorePermissionGroups -DomainController $DomainController
}
if($CopyPermissions) {
Wait-ForDirectory
Copy-ConnectorPermission -SourceConnector $SourceConnector -TargetIdentity $targetIdentity
}
}
}
else {
# target connector already exists
Write-Host 'Target connector already exists.'
if(($UpdateExistingConnector) -or ((Request-Choice -Caption ('Do you want to UPDATE the receive connector {0} on server {1}?' -f $ConnectorName, $targetName)) -eq 0)) {
$params = Get-ReceiveConnectorParameter -Connector $SourceConnector -TargetFqdn $targetFqdn -CommandName 'Set-ReceiveConnector' -IsUpdate
$params.Identity = $targetIdentity
$params.DomainController = $DomainController
if($PSCmdlet.ShouldProcess($targetIdentity, 'Update receive connector')) {
Write-Host ('Updating connector on server {0}' -f $targetName)
try {
Set-ReceiveConnector @params -ErrorAction Stop
}
catch {
Write-Warning -Message ('Failed to update receive connector {0}: {1}' -f $targetIdentity, $_.Exception.Message)
return
}
}
if($CopyPermissions) {
Copy-ConnectorPermission -SourceConnector $SourceConnector -TargetIdentity $targetIdentity
}
}
}
}
function Copy-ToAllServer {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
$SourceConnector,
[Parameter(Mandatory)]
$SourceServerObject
)
Write-Verbose -Message 'Copy receive connector to all other Exchange servers'
# All Exchange 2013+ servers hosting the Mailbox role (Exchange 2016, 2019, SE), Edge Transport servers excluded
$targetServers = @(Get-ExchangeServer -DomainController $DomainController |
Where-Object -FilterScript { ([string]$_.AdminDisplayVersion -match '^Version 15\.') -and ([string]$_.ServerRole -match 'Mailbox') -and ([string]$_.Name -ne [string]$SourceServerObject.Name) } |
Sort-Object -Property Name)
foreach($server in $targetServers) {
Copy-ToServer -SourceConnector $SourceConnector -SourceServerObject $SourceServerObject -TargetServerName ([string]$server.Name)
}
Write-Verbose -Message 'Finished copying connector to all modern Exchange servers'
}
### MAIN ----------------------------------
if($null -eq (Get-Command -Name Get-ReceiveConnector -ErrorAction SilentlyContinue)) {
Write-Warning -Message 'Exchange cmdlets are not available. Please run this script in the Exchange Management Shell.'
return
}
if((-not $CopyToAllOther) -and ($TargetServer -eq '')) {
Write-Warning -Message 'You need to either specify a dedicated target server using the -TargetServer parameter or select the -CopyToAllOther switch.'
return
}
if($ViewEntireForest) {
Write-Verbose -Message ('Setting ADServerSettings -ViewEntireForest {0}' -f $true)
Set-ADServerSettings -ViewEntireForest $true
}
$sourceServerObject = Get-ExchangeServer -Identity $SourceServer -DomainController $DomainController -ErrorAction SilentlyContinue
if($null -eq $sourceServerObject) {
Write-Warning -Message ('Source server {0} cannot be found.' -f $SourceServer)
return
}
# Read the source connector once
$sourceRC = Get-ReceiveConnector -Identity ('{0}\{1}' -f $sourceServerObject.Name, $ConnectorName) -DomainController $DomainController -ErrorAction SilentlyContinue
if($null -eq $sourceRC) {
Write-Warning -Message ('Source connector {0}\{1} cannot be accessed or does not exist!' -f $SourceServer, $ConnectorName)
return
}
if($TargetServer -ne '') {
# Copy to a single Exchange server
Copy-ToServer -SourceConnector $sourceRC -SourceServerObject $sourceServerObject -TargetServerName $TargetServer
}
else {
# Copy to all Exchange 2016/2019/SE servers
Copy-ToAllServer -SourceConnector $sourceRC -SourceServerObject $sourceServerObject
}