-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-LabUsers.ps1
More file actions
45 lines (34 loc) · 1.19 KB
/
Create-LabUsers.ps1
File metadata and controls
45 lines (34 loc) · 1.19 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
Import-Module ActiveDirectory
$csvPath = "C:\Dev\newusers.csv"
$logPath = "C:\Dev\user_creation_log.txt"
$users = Import-Csv $csvPath
$DryRun = $false
foreach ($user in $users) {
try {
$username = ($user.FirstName.Substring(0,1) + $user.LastName).ToLower()
$DomainSuffix = "lab.local"
$upn = "$username@$DomainSuffix"
$existingUser = Get-ADUser -Filter "SamAccountName -eq '$username'"
if ($existingUser) {
Add-Content $logPath "$(Get-Date -Format 'yy-MM-dd HH:mm:ss') - User $username already exists."
continue
}
$password = ConvertTo-SecureString "TempP@ss123!" -AsPlainText -Force
if (-not $DryRun) {
New-ADUser `
-Name "$($user.FirstName) $($user.LastName)" `
-GivenName $user.FirstName `
-Surname $user.LastName `
-SamAccountName $username `
-UserPrincipalName $upn `
-Department $user.Department `
-accountPassword $password `
-Enabled $true `
-Path "OU=LabUsers,DC=lab,DC=local"
}
Add-Content $logPath "$(Get-Date -Format 'yy-MM-dd HH:mm:ss') - Successfully created $username"
}
catch {
Add-Content $logPath "$(Get-Date -Format 'yy-MM-dd HH:mm:ss') - Failed to create $username - $_"
}
}