Add own implementations to code

This commit is contained in:
Ray 2024-04-12 19:02:12 +10:00 committed by GitHub
parent 597ddf94f1
commit 81bbf65a70
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 0 deletions

View File

@ -6,11 +6,24 @@
# Import required PowerShell modules
import-module ActiveDirectory
#Specify User Principal Name (Active Directory Domain Forest Name)
$UPN = "alphadelta.com"
#Get user to specify path of the CSV file containing user info to be added into the Active Directory.
$fpath = Read-Host -Prompt "Please enter the path to your CSV file:"
# Check if CSV file exists with the path specified by the end-user
# If so, error out the program with generic error stating so. (RAYYAN Contribution)
if (-not (Test-Path $fpath)) {
Write-Host "CSV file does not exist. Exiting script."
exit
}
# Display path to file given by end-user
echo $fpath
#Import users from CSV file.
$fusers = Import-Csv $fpath
#Set tempoary password to "Pa$$w0rd1" which the user will be required to change when they first login.
$fsecPass = ConvertTo-SecureString -AsPlainText "Pa$$w0rd1" -Force
@ -25,3 +38,16 @@ ForEach ($user in $fusers) {
echo $fname $lname $jtitle $OUpath
New-ADUser -SamAccountName = $fname.$lname -UserPrincipalName "$fname@alphadelta.com" -Path $OUpath -AccountPassword $fsecPass -Enabled $true -PassThru
}
# Check if user already exists within OU. Skip if so with message stating so. (RAYYAN Contribution)
if (Get-ADUser -Filter "SamAccountName -eq '$fname.$lname'") {
Write-Host "User $fname.$lname already exists. Skipping."
} else {
New-ADUser -SamAccountName "$fname.$lname" -UserPrincipalName "$fname@$UPN" -Path $OUpath -AccountPassword $fsecPass -Enabled $true -PassThru
}
}
#Print message stating the program has completed succsessfully, and to prompt them to press any key to close the program. (RAYYAN Contribution)
Read-Host -Prompt "User creation completed, press any key to close the window."