-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathInvoke-ABOffBoarding.ps1
More file actions
296 lines (261 loc) · 11.4 KB
/
Copy pathInvoke-ABOffBoarding.ps1
File metadata and controls
296 lines (261 loc) · 11.4 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
#Requires -Version 5.0
<#
.SYNOPSIS
Sample script of a offboarding process
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
.COMPONENT
Requires Module ActiveDirectory, AzureAD, MicrosoftTeams
Requires Library script OnOffBoardingLib.ps1
.LINK
.Parameter ADAccount
Active Directory Credential for remote execution without CredSSP
.Parameter ADCsvFile
Specifies the path and filename of the CSV file to remove users in Active Directory
.Parameter O365Account
Azure Active Directory Credential
.Parameter O365CsvFile
Specifies the path and filename of the CSV file to remove users in O365
.Parameter ADUsers
Specifies the users to be delete from the Active Directory
.Parameter O365Users
Specifies the users to be delete from the Azure Active Directory
.Parameter DisableUsers
Users will be disabled, not deleted
.Parameter MoveToOU
Users will be moved, not deleted
.Parameter HideInAddressbook
Users will be hides in the address lists
.Parameter RemoveMailbox
Removes user mailbox in Exchange
.Parameter RemoveMailboxPermanent
Removes user mailbox permanent in Exchange
.Parameter DisableMailbox
Disables user mailbox in Exchange
.Parameter ForwardToMailboxId
Forwards messages to this mailbox
.Parameter RemoveFromTeams
Users are removed from the MS Teams
.Parameter Delimiter
Specifies the delimiter that separates the property values in the CSV file
.Parameter FileEncoding
Specifies the type of character encoding that was used in the CSV file
.Parameter AzureDomain
Name of Azure Domain e.g. Contoso.com
.Parameter ExchangeAccount
Credential with sufficient permissions on Microsoft Exchange Server
.Parameter ExchangeServerFQDN
Specifies the Fully Qualified Domain Name of the Microsoft Exchange Server
.Parameter ExchangeRemoveFromDistributionGroups
Removes the user in the distribution groups
.Parameter DomainName
Name of Active Directory Domain
.Parameter ADAuthType
Specifies the authentication method to use
.Parameter O365Tenant
Specifies the ID of a O365 tenant
.Parameter MSTeamsAccount
MSTeams Credential
.Parameter MSTeamsTenant
Specifies the ID of a Teams tenant
#>
param(
[Parameter(Mandatory=$true,ParameterSetName = 'AD Users')]
[string[]]$ADUsers,
[Parameter(Mandatory=$true,ParameterSetName = 'O365 Users')]
[string[]]$O365Users,
[Parameter(Mandatory=$true,ParameterSetName = 'Csv Files')]
[string]$ADCsvFile,
[Parameter(Mandatory=$true,ParameterSetName = 'Csv Files')]
[string]$O365CsvFile,
[Parameter(Mandatory=$true,ParameterSetName = 'O365 Users')]
[Parameter(Mandatory=$true,ParameterSetName = 'Csv Files')]
[PSCredential]$O365Account,
[Parameter(Mandatory=$true,ParameterSetName = 'O365 Users')]
[Parameter(Mandatory =$true,ParameterSetName = 'Csv Files')]
[string]$AzureDomain,
[Parameter(ParameterSetName = 'AD Users')]
[Parameter(ParameterSetName = 'O365 Users')]
[switch]$DisableUsers,
[Parameter(ParameterSetName = 'AD Users')]
[string]$MoveToOU,
[Parameter(ParameterSetName = 'AD Users')]
[switch]$RemoveMailbox,
[Parameter(ParameterSetName = 'AD Users')]
[switch]$RemoveMailboxPermanent,
[Parameter(ParameterSetName = 'AD Users')]
[switch]$DisableMailbox,
[Parameter(ParameterSetName = 'AD Users')]
[string]$ForwardToMailboxId,
[Parameter(ParameterSetName = 'O365 Users')]
[switch]$HideInAddressbook,
[Parameter(ParameterSetName = 'O365 Users')]
[switch]$RemoveFromTeams,
[Parameter(ParameterSetName = 'Csv Files')]
[string]$Delimiter= ';',
[Parameter(ParameterSetName = 'Csv Files')]
[ValidateSet('Unicode','UTF7','UTF8','ASCII','UTF32','BigEndianUnicode','Default','OEM')]
[string]$FileEncoding = 'UTF8',
[Parameter(ParameterSetName = 'AD Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[string]$DomainName,
[Parameter(ParameterSetName = 'AD Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[PSCredential]$ADAccount,
[Parameter(ParameterSetName = 'AD Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[PSCredential]$ExchangeAccount,
[Parameter(ParameterSetName = 'AD Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[bool]$ExchangeRemoveFromDistributionGroups,
[Parameter(ParameterSetName = 'AD Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[string]$ExchangeServerFQDN,
[Parameter(ParameterSetName = 'AD Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[ValidateSet('Basic', 'Negotiate')]
[string]$ADAuthType = "Negotiate",
[Parameter(ParameterSetName = 'O365 Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[string]$O365Tenant,
[Parameter(ParameterSetName = 'O365 Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[PSCredential]$MSTeamsAccount,
[Parameter(ParameterSetName = 'O365 Users')]
[Parameter(ParameterSetName = 'Csv Files')]
[string]$MSTeamsTenant
)
Import-Module ActiveDirectory,AzureAD,MicrosoftTeams
try{
[string[]]$output = @()
# Active Directory
if($PSCmdlet.ParameterSetName -eq 'Csv Files'){
if(Test-Path -Path $ADCsvFile -ErrorAction SilentlyContinue){
$users = Import-Csv -Path $ADCsvFile -Delimiter $Delimiter -Encoding $FileEncoding -ErrorAction Stop `
-Header @('UserName','Delete','Disable','MoveTo','DisableMailbox','RemoveMailbox','RemovePermanentMailbox','ForwardToMailbox')
}
else{
Throw "$($ADCsvFile) does not exist"
}
}
elseif ($PSCmdlet.ParameterSetName -eq 'AD Users') {
[string]$del ='1'
if($DisableUsers){$del = '0'}
[string]$dis ='0'
if($DisableUsers){$dis = '1'}
[string]$delMb ='0'
if($RemoveMailbox){$delMb = '1'}
[string]$delMbPer ='0'
if($RemoveMailboxPermanent){$delMbPer = '1'}
[string]$disMb ='0'
if($DisableMailbox){$disMb = '1'}
$users = $ADUsers | ForEach-Object{[PSCustomObject]@{
UserName= $_;
Delete = $del;
Disable = $dis;
MoveTo = $MoveToOU;
RemoveMailbox = $delMb;
DisableMailbox = $disMb;
RemovePermanentMailbox = $delMbPer;
ForwardToMailbox = $ForwardToMailboxId
}
}
}
# delete users
if($null -ne $users){
if($null -eq $ExchangePassword){
$ExchangePassword = $ADPassword
}
foreach($item in $users){
if(($item.UserName -eq 'UserName') -or ([System.String]::IsNullOrWhiteSpace($item.UserName) -eq $true)){ # first line
continue
}
# off boarding the user in Exchange
if(($null -ne $ExchangeAccount) -and ([System.String]::IsNullOrWhiteSpace($ExchangeServerFQDN) -eq $false)){
if($ExchangeRemoveFromDistributionGroups){
RemoveUserFromExchangeDistributionGroups -UserName $item.UserName -ExCredential $ExchangeAccount -ServerName $ExchangeServerFQDN
$output += "User $($item.UserName) removed from distribution groups"
}
OffBoardExchangeMailbox -ServerName $ExchangeServerFQDN -ExCredential $ExchangeAccount -UserName $item.UserName `
-Delete $item.RemoveMailbox -DeletePermanent $item.RemovePermanentMailbox -Disable $item.DisableMailbox `
-ForwardTo $item.ForwardToMailbox
$output += "User $($item.UserName) Mailbox disabled/removed"
}
else{
$output += "User $($item.UserName) can´t disbale/remove Mailbox. Missing Parameters"
}
# delete users in Active Directory
DeleteUserInAD -User $item.UserName -Delete $item.Delete -Disable $item.Disable `
-MoveToOU $Item.MoveTo `
-ADCredential $ADAccount -DomainName $DomainName -AuthType $ADAuthType
$output += "User $($item.UserName) in Active Directory off boarded"
}
}
$users = $null
# Azure Active Directory / O365
if($PSCmdlet.ParameterSetName -eq 'Csv Files'){
if(Test-Path -Path $O365CsvFile -ErrorAction SilentlyContinue){
$users = Import-Csv -Path $O365CsvFile -Delimiter $Delimiter -Encoding $FileEncoding -ErrorAction Stop `
-Header @('UserName','Delete','Disable','HideInAddressLists','RemoveFromTeams')
}
}
elseif($PSCmdlet.ParameterSetName -eq 'O365 Users'){
[string]$del ='1'
if($DisableUsers){$del = '0'}
[string]$dis ='0'
if($DisableUsers){$dis = '1'}
[string]$hide ='0'
if($HideInAddressbook){$hide = '1'}
[string]$remo ='0'
if($RemoveFromTeams){$remo='1'}
$users = $O365Users | ForEach-Object{[PSCustomObject]@{
UserName= $_;
Delete = $del;
Disable = $dis;
HideInAddressLists = $hide
RemoveFromTeams = $remo}}
}
if($null -ne $users){
# Delete users in Azure AD
[string]$o365Domain = '@' + $AzureDomain
foreach($item in $users){
if(($item.UserName -eq 'UserName') -or ([System.String]::IsNullOrWhiteSpace($item.UserName) -eq $true)){ # first line
continue
}
$tmp = $item.UserName + $o365Domain
# user removes from all MS Teams
if($item.RemoveFromTeams -eq '1'){
if($null -eq $MSTeamsAccount){
$MSTeamsAccount = $O365Account
}
if([System.String]::IsNullOrWhiteSpace($MSTeamsTenant) -eq $true){
$MSTeamsTenant = $Tenant
}
RemoveO365UserFromTeams -UserName $tmp -TeamsCredential $MSTeamsAccount -TeamsTenant $MSTeamsTenant
$output += "User $($item.UserName) in removed from all MS Teams"
}
# delete users in Active Directory
DeleteUserInO365 -O365Credential $O365Account -Tenant $O365Tenant -UserName $tmp `
-Delete $item.Delete -Disable $item.Disable -HideInAddressLists $item.HideInAddressLists
$output += "User $($item.UserName) in O365 off boarded"
}
}
if($SRXEnv) {
$SRXEnv.ResultMessage = $output
}
else {
Write-Output $output
}
}
catch{
Write-Output $_.exception.message
throw
}
finally{
}