I have an Exchange Server that forwards a large number of domains to a smart host using a send connector. I have written 2 little powershell scripts to add or remove these domains from my Exchange configuration. The tricky part was the "AddressSpaces" of the "Set-Sendconnector" but I think I've found a pretty nice solution.
AddExtDomain.ps1
Param(
[string] $DomainName
)
new-AcceptedDomain -Name $DomainName -DomainName $DomainName -DomainType 'ExternalRelay'
new-RemoteDomain -Name $DomainName -DomainName $DomainName
$a = (Get-SendConnector "Smart Host Sendmail").AddressSpaces
$a += $DomainName
Set-SendConnector "Smart Host Sendmail" -AddressSpaces $a
RemExtDomain.ps1
Param(
[string] $DomainName
)
Remove-RemoteDomain -Identity $DomainName -confirm:$false
Remove-AcceptedDomain -Identity $DomainName -confirm:$false
$a = (Get-SendConnector "Smart Host Sendmail").AddressSpaces | where {$_.Domain -ne $DomainName}
Set-SendConnector "Smart Host Sendmail" -AddressSpaces $a
If you want to add multiple domains at a time, this solution, using import from a csv-file, is better for you:
To get the right format for the CSV file, one can export the current domains using this script:
ExpExtDomains.ps1
(Get-SendConnector "Smart Host Sendmail").AddressSpaces | Export-Csv AddressSpaces.csv
This is what the list will look like. Now I can modify the list by replacing the values with my new domains.
Finally, to import domains from that list, I use this script:
ImpExtDomains.ps1
import-csv AddressSpaces.csv | % { new-AcceptedDomain -Name $_.Domain -DomainName $_.Domain -DomainType 'ExternalRelay' }
import-csv AddressSpaces.csv | % { new-RemoteDomain -Name $_.Domain -DomainName $_.Domain }
$a = (Get-SendConnector "Smart Host Sendmail").AddressSpaces
import-csv AddressSpaces.csv | % { $a += $_.Domain }
Set-SendConnector "Smart Host Sendmail" -AddressSpaces $a
To confirm the settings:
Get-AcceptedDomain
Get-RemoteDomain
(Get-SendConnector "Smart Host Sendmail").AddressSpaces