-
Notifications
You must be signed in to change notification settings - Fork 0
/
disable.ps1
141 lines (131 loc) · 5.05 KB
/
disable.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
#Initialize default properties
$c = $configuration | ConvertFrom-Json
$p = $person | ConvertFrom-Json;
$aRef = $accountReference | 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 $aRef
if ($mailbox.Name.Count -eq 0) {
Write-Verbose "Could not find mailbox with identity '$($aRef)'"
$auditLogs.Add([PSCustomObject]@{
Message = "$action mailbox for: [$($p.DisplayName)] will be executed."
IsError = $false
})
}
if ($mailbox.Name.Count -gt 0) {
Write-Verbose "Mailbox found for: [$($p.DisplayName)]"
$action = 'Hide'
$auditLogs.Add([PSCustomObject]@{
Message = "$action mailbox for: [$($p.DisplayName)] will be executed."
IsError = $false
})
}
$dryRun = $false
if (-not($dryRun -eq $true)) {
switch ($action) {
'Hide' {
Set-RemoteMailbox -identity $aRef -HiddenFromAddressListsEnabled $true
$success = $true
$auditLogs.Add([PSCustomObject]@{
Message = "Hiding mailbox for: [$($p.DisplayName)] was successful."
IsError = $false
})
break
}
}
}
}
}
catch {
$errorMessage = "Failed to unhide 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