From c976387c36fb6978aed079395b974f67e71e112d Mon Sep 17 00:00:00 2001 From: reiyua Date: Fri, 12 Apr 2024 23:41:26 +1000 Subject: [PATCH] Update AutoUserADCreator.ps1 --- AutoUserADCreator.ps1 | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/AutoUserADCreator.ps1 b/AutoUserADCreator.ps1 index cc55fda..abe4418 100644 --- a/AutoUserADCreator.ps1 +++ b/AutoUserADCreator.ps1 @@ -8,9 +8,6 @@ # Import required PowerShell modules import-module ActiveDirectory -#Specify User Principal Name (Active Directory Domain Forest Name) (Rayyan Modified to allow for user to enter their own forest name) -$UPN = Read-Host -Prompt "Please enter the Active Directory Forest name (example.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 containing user info to be added to the OU's within the Active Directory Domain Forest:" @@ -37,22 +34,25 @@ ForEach ($user in $fusers) { $fname = $user.fName $lname = $user.lName $jtitle = $user.jTitle - $OUpath = $user.OU + $OUpath = $user.OuPath 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) # Source for code (https://morgantechspace.com/2016/11/check-if-ad-user-exists-with-powershell.html) - if (Get-ADUser -Filter "SamAccountName -eq '$fname.$lname'") { - Write-Host "User $fname.$lname already exists. Skipping." + 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 + # Construct the full name for user + $fullName = "$fname $lname" + # Create the user with full name now defined + new-ADUser -Name $fullName -SamAccountName ($fname + $lname) -UserPrincipalName "$fname@PSTest.local" -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) # Source for code (https://www.thomasmaurer.ch/2021/01/how-to-add-sleep-wait-pause-in-a-powershell-script/#:~:text=Read%2DHost%20%2DPrompt%20%22Press%20any%20key%20to%20continue...%22) Read-Host -Prompt "User creation completed, press any key to close the window." +`