-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.ps1
150 lines (140 loc) · 5.67 KB
/
create.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
#Initialize default properties
$c = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json;
$success = $False;
$auditLogs = [Collections.Generic.List[PSCustomObject]]@()
#config mapping
$exchangeConnectionUri = $c.exchange.url
$exchangeAdminUsername = $c.exchange.username
$exchangeAdminPassword = $c.exchange.password
$authenticationmode = $c.exchange.authenticationmode
$remoteroutingdomain = $c.exchange.remoteroutingaddress
$skipcacheck = $c.exchange.skipcacheck
$skipcncheck = $c.exchange.skipcncheck
$skiprevocationcheck = $c.exchange.skiprevocationcheck
#account mapping
$account = @{
samaccountname = $p.Accounts.MicrosoftActiveDirectory.sAMAccountName
mailaddress = $p.Accounts.MicrosoftActiveDirectory.Mail
targetaddress = ($p.Accounts.MicrosoftActiveDirectory.Mail).Split("@")[0] + "@" + $remoteroutingdomain
}
function EstablishSession {
try {
$adminSecurePassword = ConvertTo-SecureString -String "$exchangeAdminPassword" -AsPlainText -Force
$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $exchangeAdminUsername, $adminSecurePassword
$sessionOption = New-PSSessionOption -SkipCACheck:$skipcacheck -SkipCNCheck:$skipcncheck -SkipRevocationCheck:$skiprevocationcheck
$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $exchangeConnectionUri -Credential $adminCredential -Authentication $authenticationmode -SessionOption $sessionOption -ErrorAction Stop
$null = Import-PSSession $session -DisableNameChecking -AllowClobber
$auditLogs.Add([PSCustomObject]@{
Message = "Successfully connected to Exchange using the URI [$exchangeConnectionUri]"
IsError = $false
})
}
catch {
$errorMessage = "Error connecting to Exchange using the URI [$exchangeConnectionUri]"
Write-Verbose $errorMessage
$auditLogs.Add([PSCustomObject]@{
Message = $errorMessage
IsError = $true
})
}
return $session
}
function DisconnnectSession {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
$session
)
try {
Remove-PsSession -Session $session -Confirm:$false -ErrorAction Stop
$auditLogs.Add([PSCustomObject]@{
Message = "Successfully disconnected from Exchange."
IsError = $false
})
}
catch {
$errorMessage = "Error disconnecting from Exchange. Error: $($_.Exception.Message)"
Write-Verbose $errorMessage
$auditLogs.Add([PSCustomObject]@{
Message = $errorMessage
IsError = $true
})
}
}
try {
$exchangeSession = EstablishSession
if ($null -eq $exchangeSession) {
$auditLogs.Add([PSCustomObject]@{
Message = "No session available to perform any mailbox actions."
IsError = $true
})
}
if ($exchangeSession) {
$mailbox = Get-RemoteMailbox -Identity $account.samaccountname
if ($mailbox.Name.Count -eq 0) {
Write-Verbose "Could not find mailbox with identity '$($account.samaccountname)'"
$action = 'Create'
$auditLogs.Add([PSCustomObject]@{
Message = "$action mailbox for: [$($p.DisplayName)] will be executed."
IsError = $false
})
}
if ($mailbox.Name.Count -gt 0) {
Write-Verbose "Correlation found mailbox for: [$($p.DisplayName)]"
$action = 'Correlate'
$auditLogs.Add([PSCustomObject]@{
Message = "$action mailbox for: [$($p.DisplayName)] will be executed."
IsError = $false
})
}
if (-not($dryRun -eq $true)) {
switch ($action) {
'Create' {
Enable-RemoteMailbox -identity $account.samaccountname -RemoteRoutingAddress $account.targetaddress -PrimarySmtpAddress $account.mailaddress
$accountReference = $account.samaccountname
$success = $true
$auditLogs.Add([PSCustomObject]@{
Message = "Creating mailbox for: [$($p.DisplayName)] was successful. AccountReference is: [$accountReference]"
IsError = $false
})
break
}
'Correlate' {
$accountReference = $account.samaccountname
$success = $true
$auditLogs.Add([PSCustomObject]@{
Message = "Correlating mailbox for: [$($p.DisplayName)] was successful. AccountReference is: [$accountReference]"
IsError = $false
})
break
}
}
}
}
}
catch {
$errorMessage = "Failed to create mailbox for: [$($p.DisplayName)]. Error: $($_Exception.Message)"
Write-Verbose $errorMessage
$auditLogs.Add([PSCustomObject]@{
Message = $errorMessage
IsError = $true
})
}
finally {
if ($exchangeSession) {
DisconnnectSession $exchangeSession
}
}
$result = [PSCustomObject]@{
Success = $success
AccountReference = $accountReference
AuditLogs = $auditLogs
Account = $account
# Optionally return data for use in other systems
ExportData = [PSCustomObject]@{
Identity = $accountReference
PrimaryMail = $account.mailaddress
}
}
Write-Output $result | ConvertTo-Json -Depth 10