-
Notifications
You must be signed in to change notification settings - Fork 0
/
V2.NewUserProvisioning.Functions.ps1
485 lines (392 loc) · 23.2 KB
/
V2.NewUserProvisioning.Functions.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<#
############################################################################################################
# Pierre Correia # Description: User Provisioning Functions #
# User Provisioning Functions # #
# Updateded: 02/13/2019 # #
############################################################################################################
#>
# Service Desk Menu
function Show-Menu
{
param (
[string]$Title = 'Service Desk Menu'
)
cls
Write-Host "================ $Title ================"
Write-Host "[1] Create a new user"
Write-Host "[2] Create a new Distribution List"
Write-Host "[3] Create a new Shared Mailbox"
Write-Host "[4] Migrate On-Prem mailbox to Office365"
Write-Host "[5] Assign license to Office 365 mailbox"
Write-Host "[6] Synchronize Active Directory Servers"
Write-Host "[7] Disable User"
Write-Host "[Q] Quit Menu"
}
# Initializing a Countdown Function
Function Start-Countdown
{
Param(
[Int32]$Seconds = 30,
[string]$Message = "Pausing for 30 seconds..."
)
ForEach ($Count in (1..$Seconds))
{
Write-Progress -Id 1 -Activity $Message -Status "Time Left: $Seconds seconds, $($Seconds - $Count)" -PercentComplete (($Count / $Seconds) * 100)
Start-Sleep -Seconds 1
}
Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed
}
# Force Azure Directory Services Sync
Function Start-DeltaSync
{
$session = New-PSSession -ComputerName $AzureADServer
Invoke-Command -Session $session -ScriptBlock {Start-ADSyncSyncCycle Delta}
Remove-PSSession $session
}
# Apply O365 licenses to user
Function O365License
{
import-module MSOnline
# Connect to MS Online Service
$MsolSession = Connect-MsolService -Credential $UPNCredentials
# Retrieve Username
$Username = Read-Host -Prompt "Enter User's username. Example; jdoe"
$UPN = $Username + $FullDomain
# Check if the user has been replicated to Office365
do {
$CheckIfUserAccountExists = Get-MsolUser -UserPrincipalName $UPN -ErrorAction SilentlyContinue
Write-Host "Checking if $UPN has been migrated to Office365. Waiting 15 seconds."
Start-Countdown -Seconds 15 -Message "Checking if $UPN has been migrated to Office365. Waiting 15 seconds."
}
While ($CheckIfUserAccountExists -eq $Null)
# Set Regional Setting and License
Set-MsolUser -Userprincipalname $UPN -UsageLocation $O365RegionCode
Set-MsolUserLicense -User $UPN -AddLicenses $O365ProductSKU
}
# Create Distribution List
Function CreateDistributionList
{
# Prompt User for Distribution List information
Write-Host "Enter Distribution List Name. Example: IT Support Staff" -ForegroundColor Yellow
$MailboxName = Read-Host -Prompt "Distribution List Name"
Write-Host "Enter Distribution List Display Name. Example: IT Support Staff" -ForegroundColor Yellow
$MailboxDisplayName = Read-Host -Prompt "Distribution List Display Name"
Write-Host "Enter Distribution List Alias. Example: itsupportstaff" -ForegroundColor Yellow
$MailboxAlias = Read-Host -Prompt "Distribution List Alias"
$MailboxAlias = $MailboxAlias.ToLower().Trim()
# Verify if sam account already exists in the domain
$TmpUser = $(try {Get-ADUser $MailboxAlias} catch {$null})
# Loop until we get a sam account that is not in the domain
while ($TmpUser -ne $null)
{
Write-Host "$MailboxAlias is already in use, please enter Alias manually."
$MailboxAlias = Read-Host -Prompt "Enter Distribution List Alias. Example: itsupportstaff"
$MailboxAlias = $MailboxAlias.ToLower().Trim()
$TmpUser = $(try {Get-ADUser $MailboxAlias} catch {$null})
}
Write-Host "Enter Distribution List Description. Example: Mailbox for IT Support Staff" -ForegroundColor Yellow
$MailboxDescription = Read-Host -Prompt "Distribution List Description"
<#----------------------------------- End of Input Logic -----------------------------------#>
# Connect to Exchange On-Prem
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $OnPremURI -Credential $DomainCredentials
Import-PSSession $ExSession -AllowClobber
# Create Distribution List
New-DistributionGroup -Name $MailboxName -DisplayName $MailboxDisplayName -Alias $MailboxAlias -OrganizationalUnit $DistListOU
# Replicate between internal domain controllers
Write-Host
Write-Host
Write-Host "Replicating to Domain Controllers. Waiting 30 seconds."
Write-Host
Write-Host
Start-Countdown -Seconds 30 -Message "Replicating to Domain Controllers. Waiting 30 seconds."
# Set the desccription
Set-ADGroup -identity $MailboxName -Description $MailboxDescription
# Forcing Azure Directory Synchronization with Domain Controllers
Start-DeltaSync
# Disconnect Exhange sessions
Remove-PSSession $ExSession
}
# Create Shared Mailbox
Function CreateSharedMailbox
{
# Prompt User for Shared Mailbox information
Write-Host "Enter Mailbox Name. Example: IT Support Staff" -ForegroundColor Yellow
$MailboxName = Read-Host -Prompt "Distribution List Name"
Write-Host "Enter Mailbox Display Name. Example: IT Support Staff" -ForegroundColor Yellow
$MailboxDisplayName = Read-Host -Prompt "Distribution List Display Name"
Write-Host "Enter Mailbox Alias. Example: itsupportstaff" -ForegroundColor Yellow
$MailboxAlias = Read-Host -Prompt "Distribution List Alias"
$MailboxAlias = $MailboxAlias.ToLower().Trim()
# Verify if sam account already exists in the domain
$TmpUser = $(try {Get-ADUser $MailboxAlias} catch {$null})
# Loop until we get a sam account that is not in the domain
while ($TmpUser -ne $null)
{
Write-Host "$MailboxAlias is already in use, please enter Alias manually."
$MailboxAlias = Read-Host -Prompt "Enter Distribution List Alias. Example: itsupportstaff"
$MailboxAlias = $MailboxAlias.ToLower().Trim()
$TmpUser = $(try {Get-ADUser $MailboxAlias} catch {$null})
}
Write-Host "Enter Mailbox Description. Example: Mailbox for IT Support Staff" -ForegroundColor Yellow
$MailboxDescription = Read-Host -Prompt "Distribution List Description"
# Set the UPN
$UPN = $MailboxAlias + $FullDomain
<#----------------------------------- End of Input Logic -----------------------------------#>
# Connect to Exchange On-Prem
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $OnPremURI -Credential $DomainCredentials
Import-PSSession $ExSession -AllowClobber
# Create Mailbox
New-Mailbox -Shared -Name $MailboxName -DisplayName $MailboxDisplayName -Alias $MailboxAlias -Database $MailboxDatabase -OrganizationalUnit $SharedMBOU
# Replicate between internal domain controllers
Write-Host
Write-Host
Write-Host "Replicating to Domain Controllers. Waiting 30 seconds."
Write-Host
Write-Host
Start-Countdown -Seconds 30 -Message "Replicating to Domain Controllers. Waiting 30 seconds."
# Set the desccription
Set-ADUser $MailboxName -Description $MailboxDescription
# Forcing Azure Directory Synchronization with Domain Controllers
Start-DeltaSync
# Connect to Exchange O365
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $O365URI -Credential $UPNCredentials -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
# Check if the user has been migrated to Office365
do {
$CheckIfUserAccountExists = Get-MsolUser -UserPrincipalName $UPN -ErrorAction SilentlyContinue
Write-Host "Checking if $UPN has been migrated to Office365. Waiting 15 seconds."
Start-Countdown -Seconds 15 -Message "Checking if $UPN has been migrated to Office365. Waiting 15 seconds."
}
While ($CheckIfUserAccountExists -eq $Null)
# Migrate mailbox to O365 using the information from the keyboard input
New-MoveRequest -Remote -RemoteHostName $OnPremExRemoteHostName -RemoteCredential $DomainCredentials -TargetDeliveryDomain $O365TenantName -Identity $UPN -BadItemLimit 200
# End of Script Notification
Write-Host "Provisioning has completed." -ForegroundColor Green
# Disconnect Exhange sessions
Remove-PSSession $ExSession
Remove-PSSession $O365Session
}
# Migrate Mailbox to Office 365
Function MigrateMailboxO365
{
# Connect to Exchange On-Prem
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $OnPremURI -Credential $DomainCredentials
Import-PSSession $ExSession -AllowClobber
# Connect to Exchange O365
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $O365URI -Credential $UPNCredentials -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
# Connect to Microsoft Online Services
$MsolSession = Connect-MsolService -Credential $UPNCredentials
# Retrieve Username
$Username = Read-Host -Prompt "Enter User's username. Example; jdoe"
$UPN = $Username + $FullDomain
# Check if the user has been replicated to Office365
do {
$CheckIfUserAccountExists = Get-MsolUser -UserPrincipalName $UPN -ErrorAction SilentlyContinue
Write-Host "Checking if $UPN has been synced to Office365. Waiting 15 seconds."
Start-Countdown -Seconds 15 -Message "Checking if $UPN has been synced to Office365. Waiting 15 seconds."
}
While ($CheckIfUserAccountExists -eq $Null)
# Migrate user
New-MoveRequest -Remote -RemoteHostName $OnPremExRemoteHostName -RemoteCredential $DomainCredentials -TargetDeliveryDomain $O365TenantName -Identity $UPN -BadItemLimit 200
# Assign User Regional Setting and License
Set-MsolUser -Userprincipalname $UPN -UsageLocation $O365RegionCode
Set-MsolUserLicense -User $UPN -AddLicenses $O365ProductSku
# End of Script Notification
Write-Host "User Migration script has completed." -ForegroundColor Green
# Disconnect Exhange sessions
Remove-PSSession $ExSession
Remove-PSSession $O365Session
}
# Create User Account
Function CreateUserAccount
{
# Prompt the user for the account to copy
$UserToCopyInput = Read-Host -Prompt "`nEnter the existing user account to copy"
$UserToCopy = $(try {Get-ADUser $UserToCopyInput} catch {$null})
# If we could not get anything keep looping until we do
while($UserToCopy -eq $null)
{
Write-Host "Could not find $UserToCopyInput, try again."
$UserToCopyInput = Read-Host -Prompt "User to copy"
$UserToCopy = $(try {Get-ADUser $UserToCopyInput} catch {$null})
}
#Loop until first name is given
while($GivenName -eq "")
{
$GivenName = Read-Host -Prompt "Enter First Name"
}
$GivenName = $GivenName.Trim()
# Loop until last name is given
while($SurName -eq "")
{
$SurName = Read-Host -Prompt "Enter Last Name"
}
$SurName = $SurName.Trim()
# Set the Whole name and display name
$Name = $DisplayName = $GivenName + " " + $SurName
#Try to create SamAccountName and UPN based off first and last name
$SamAccountName = $GivenName.Substring(0,1) + $SurName
# Try to see if the sam account already exists in the domain
$TmpUser = $(try {Get-ADUser $SamAccountName} catch {$null})
# Loop until we get a sam account that is not in the domain
while ($TmpUser -ne $null)
{
Write-Host "$SamAccountName is already in AD, please enter SamAccount manually."
$SamAccountName = Read-Host -Prompt "SamAccount"
$TmpUser = $(try {Get-ADUser $SamAccountName} catch {$null})
}
$SamAccountName = $SamAccountName.ToLower()
# Set the UPN based off the Sam Account Name
$UPN = $SamAccountName + $FullDomain
# Set the path for our user object from the user to copy
$userinstance = Get-ADUser -Identity $UserToCopyInput
$DN = $userinstance.distinguishedName
$OldUser = [ADSI]"LDAP://$DN"
$Parent = $OldUser.Parent
$OU = [ADSI]$Parent
$OUDN = $OU.distinguishedName
# Set the Department, Title, and Company from the User to Copy
$DeptTitleComp = Get-ADUser -Identity $UserToCopyInput -Properties Department,Title,Company
# Prompt user for Office info
while($Office -eq "")
{
$Office = Read-Host -Prompt "Enter Office"
}
# Prompt user for Phone info
while($Phone -eq "")
{
$Phone = Read-Host -Prompt "Enter Phone Number"
}
# Prompt user for SSN info
while($SSN -eq "")
{
$SSN = Read-Host -Prompt "Enter SSN"
}
# Prompt user for Password
while($Password -eq "")
{
$Password = Read-Host -Prompt "Enter Password"
}
# Create the AD User
New-ADUser `
-SamAccountName $SamAccountName `
-UserPrincipalName $UPN `
-Name $Name `
-DisplayName $DisplayName `
-GivenName $GivenName `
-SurName $SurName `
-Department $DeptTitleComp.Department `
-Office $Office `
-OfficePhone $Phone `
-Title $DeptTitleComp.Title `
-Description $DeptTitleComp.Title `
-Company $DeptTitleComp.Company `
-POBox $SSN `
-Path "$OUDN" `
-AccountPassword (ConvertTo-SecureString "$Password" -AsPlainText -force) `
-Enabled $True
# Prompt user for Manager info
while($Manager -eq "")
{
$Manager = Read-Host -Prompt "Enter Manager"
$MgrObject = $(try {Get-ADUser -Filter "displayName -like '$($Manager)'"} catch {$null})
# This block executes if the display name is entered
if ($MgrObject -ne $null)
{
Set-ADUser -Identity $SamAccountName -Manager $MgrObject.SamAccountName
}
# This block executes if the sam account name is entered
else
{
Set-ADUser -Identity $SamAccountName -Manager $Manager
}
}
<#----------------------------------- End of Input Logic -----------------------------------#>
# Copies group membership(s) to the new user
Get-ADUser -Identity $UserToCopy -Properties memberof |
Select-Object -ExpandProperty memberof |
Add-ADGroupMember -Members $SamAccountName
# Countdown for 30 seconds to allow Domain Controllers to replicate.
Write-Host
Write-Host
Write-Host "Replicating Domain Controllers. Waiting 30 seconds.`n "
Write-Host
Write-Host
Start-Countdown -Seconds 30 -Message "Replicating Domain Controllers. Waiting 30 seconds."
# Connect to Exchange On-Prem
$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $OnPremURI -Credential $DomainCredentials
Import-PSSession $ExSession -AllowClobber
# Create a Mailbox for User
try {Enable-Mailbox -Identity $SamAccountName -Database $MailboxDatabase -RetentionPolicy 'DeletedItemsPolicy'} catch {ErrorAction 'SilentlyContinue'}
# Countdown 30 seconds for Exchange to finalize mailbox creation
Write-Host
Write-Host
Write-Host "Finalizing Mailbox creation. Waiting 30 seconds.`n "
Write-Host
Write-Host
Start-Countdown -Seconds 30 -Message "Finalizing Mailbox creation. Waiting 30 seconds"
# Forcing Azure Directory Synchronization with Domain Controllers
Start-DeltaSync
# Connect to Exchange O365
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $O365URI -Credential $UPNCredentials -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
# Connect to Microsoft Online Services
$MsolSession = Connect-MsolService -Credential $UPNCredentials
# Check if the user has been replicated to Office365
do {
$CheckIfUserAccountExists = Get-MsolUser -UserPrincipalName $UPN -ErrorAction SilentlyContinue
Write-Host "Checking if $UPN has been migrated to Office365. Waiting 15 seconds."
Start-Countdown -Seconds 15 -Message "Checking if $UPN has been migrated to Office365. Waiting 15 seconds."
}
While ($CheckIfUserAccountExists -eq $Null)
# Migrate user
New-MoveRequest -Remote -RemoteHostName $OnPremExRemoteHostName -RemoteCredential $DomainCredentials -TargetDeliveryDomain $O365TenantName -Identity $UPN -BadItemLimit 200
# Assign User Regional Setting and License
Set-MsolUser -Userprincipalname $UPN -UsageLocation $O365RegionCode
Set-MsolUserLicense -User $UPN -AddLicenses $O365ProductSku
# Disable ActiveSync
Start-Countdown -Seconds 60 -Message "Applying License. Waiting 60 seconds."
Set-CASMailbox -Identity $UPN -ActiveSyncEnabled $False
# End of Script Notification
Write-Host "User Provisioning script has completed." -ForegroundColor Green
# Disconnect Exhange sessions
Remove-PSSession $ExSession
Remove-PSSession $O365Session
}
# Disable User Account
Function DisableUserAccount
{
# Import the AD Powershell Module
Import-Module activedirectory
# Check for valid username
do {
$UserToDeprovision = Read-Host -Prompt "Enter the SamAccount to deprovision"
$user = $(try{Get-ADUser $UserToDeprovision} catch {$null})
}
While ($user -eq $null)
# Confirm user selection
$UserDecision = Read-Host -Prompt "Are you sure you want to deprovision $UserToDeprovision (Yes/N)"
# Proceed if user types "Yes", else restart process.
if ($UserDecision -eq "Yes")
{
$DATE = Get-Date
# Connect to Exchange O365
$O365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $O365URI -Credential $UPNCredentials -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
# Remove Address from GAL
$UPN = $UserToDeprovision + $FullDomain
Set-Mailbox -Identity $UPN -HiddenFromAddressListsEnabled $true
# Disconnect Exchange session
Remove-PSSession $O365Session
# Set user account to disabled and add the managers name to notes
$UserManager= Get-ADUser(Get-ADUser $UserToDeprovision -Properties Manager).manager -Properties DisplayName
Set-ADUser $UserToDeprovision -Replace @{Info="Employee's Manager: $UserManager"}
Set-ADUser -Identity $UserToDeprovision -Enabled $false -Description "Disabled Date: $DATE" -Manager $null
# Set the users password to a random password
Set-ADAccountPassword -Identity $UserToDeprovision -NewPassword (ConvertTo-SecureString "7b?Ze~Mh#C'?UN6C'b@x" -AsPlainText -force)
}
else {DisableUserAccount}
# Force Azure Directory Services Sync
Start-DeltaSync
}