-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet-ExchangeOnlineAliases.ps1
109 lines (90 loc) · 4.06 KB
/
Set-ExchangeOnlineAliases.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
<#
.SYNOPSIS
Sets mailbox aliases for Cloud-only users in Exchange Online based on a CSV.
.DESCRIPTION
Imports the identity & aliases combination from a CSV.
Uses Set-Mailbox to apply changes to Exchange Online mailboxes.
Defaults to adding new aliases, but can also overwrite existing aliases.
.PARAMETER CSVFile
Path to the CSV file containing columns 'Identity' and 'Aliases'.
For multiple aliases, comma separate them in the same field/cell.
.PARAMETER Overwrite
If switch is present/true, then any existing 'smtp' (alias) email addresses will be replaced with the CSV contents.
Any other values in the EmailAddresses field are preserved, Eg. SMTP (Primary), SIP, X500.
.EXAMPLE
.\Set-ExchangeOnlineAliases.ps1 -WhatIf
An Exchange Online PowerShell session is already active
What if: Setting mailbox Identity:"[email protected]".
What if: Setting mailbox Identity:"Example2".
What if: Setting mailbox Identity:"acd37fa8-d5d8-49b4-38f4-ba0af2a9139a".
.EXAMPLE
.\Set-ExchangeOnlineAliases.ps1 -CSVFile EXO_Aliases.csv -Overwrite
An Exchange Online PowerShell session is already active
WARNING: The command completed successfully but no settings of 'Example1' have been modified.
WARNING: Example2: Alias [email protected] not present in CSV, will be removed from EXO
WARNING: Example2: Alias [email protected] not present in CSV, will be removed from EXO
WARNING: Example3: Alias [email protected] not present in CSV, will be removed from EXO
#>
[CmdletBinding(SupportsShouldProcess)]
param (
[ValidateScript({ Test-Path $_ })]
[string]$CSVFile = 'Aliases.csv',
[switch]$Overwrite
)
function Start-ExchangeOnlineSession {
if (Get-PSSession | Where-Object { ($_.State -eq 'Opened') -and $_.ConnectionUri -match 'https://outlook.office365.com/*' }) {
Write-Host 'An Exchange Online PowerShell session is already active' -ForegroundColor Green
}
else {
Write-Host 'Initiating Exchange Online PowerShell session' -ForegroundColor Yellow
# https://docs.microsoft.com/en-us/powershell/exchange/connect-to-exchange-online-powershell
Connect-ExchangeOnline -ShowBanner:$false
}
}
#Requires -Modules ExchangeOnlineManagement
Start-ExchangeOnlineSession
$csvContent = Import-Csv $CSVFile -ErrorAction Stop
:ItemLoop Foreach ($item in $csvContent) {
try {
# Get the mailbox for the current row of the CSV
$mailbox = Get-EXOMailbox -Identity $item.Identity -ErrorAction Stop
}
catch {
Write-Error $_
Continue ItemLoop
}
# Find all current aliases in the EmailAddresses field (lowercase smtp), and remove the 'smtp:'
$currentAliases = ($mailbox.EmailAddresses -cmatch '^smtp:.*$') -replace 'smtp:', ''
# Find all addresses in the EmailAddresses field that are not 'smtp:'
$otherAddresses = ($mailbox.EmailAddresses -cmatch '(?<!smtp):.*')
# Split value from Aliases cell into multiple strings and remove whitespace
$futureAliases = $item.Aliases -split ',' -replace '\s+', ''
# Convert CSV aliases (comma separated) into an object of smtp: emails
$futureAliasStrings = $futureAliases | ForEach-Object { 'smtp:', $_ -join '' }
$params = @{
Identity = $item.Identity
ErrorAction = 'Stop'
WhatIf = $WhatIfPreference
}
if ($Overwrite) {
# Check for any aliases not present in the CSV, but currently present in Exchange Online
$currentAliases | ForEach-Object {
if ($_ -notin $futureAliases) {
Write-Warning ("{0}: Alias {1} not present in CSV, will be removed from EXO" -f $mailbox.Name, $_)
}
}
# Replace current EmailAddress field with all original SMTP (Primary), SIP, X500 etc values, plus new smtp (alias) values
$params.EmailAddress = $otherAddresses + $futureAliasStrings
}
else {
# Add to existing EmailAddress field
$params.EmailAddress = @{Add = $futureAliasStrings }
}
try {
Set-Mailbox @params
}
catch {
Write-Error $_
Continue ItemLoop
}
}