Forum Discussion
Jord9857
Nov 01, 2021Copper Contributor
Creating Users with a CSV
Hi all, I'm trying to create a script which will pull user's info from a .csv, check the user's DisplayName with AD to make sure a user with that DisplayName doesn't already exist, does exact sam...
Jord9857
Nov 02, 2021Copper Contributor
Hi,
Thank you for your response.
However even making this adjustment to the .csv, it still doesn't work properly.
firstname lastname displayname jobtitle telephone department username SamAccountName email password OU
Fred Jones Fred Jones Maths Teacher 987654321 Maths Fred.Jones Fred.Jones Fred.Jones@Jord.Local Hello2021! OU=Teaching Staff,OU=Users,OU=Test,DC=Jord,DC=Local
Fred Try Fred Try Administrator 123456789 Admin Fred.Try Fred.Try Fred.Try@Jord.Local Hello2021! OU=Admin Staff,OU=Users,OU=Test,DC=Jord,DC=Local
Ren Jones Ren Jones English Teacher 134258679 English Ren.Jones Ren.Jones Ren.Jones@Jord.Local Hello2021! OU=Customers,OU=Users,OU=Test,DC=Jord,DC=Local
Sophie Hop Sophie Hop Lanuages Teacher 174392834 Languages Sophie.Hop Sophie.Hop Sophie.Hop@Jord.Local Hello2021! OU=Other,OU=Users,OU=Test,DC=Jord,DC=Local
This is the first output (user's don't exist) - it works completely fine for the user Sophie Hop but same issue with the other user's
STARTED SCRIPT
Get-AdUser : Variable: 'displayname' found in expression: $displayname is not defined.
At test.ps1:15 char:11
+ $ADUser = Get-AdUser -Filter {DisplayName -eq $displayname}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Get-AdUser : Variable: 'SamAccountName' found in expression: $SamAccountName is not defined.
At test.ps1:19 char:11
+ $ADUser = Get-AdUser -Filter {SamAccountName -eq $SamAccountName}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Fred Jones account was successfully created
Fred Jones already exists
Fred.Jones already exists
Fred Try account was successfully created
Fred Try already exists
Fred.Try already exists
Ren Jones account was successfully created
Ren Jones already exists
Ren.Jones already exists
Sophie Hop account was successfully created
FINISHED SCRIPT
This is the 2nd output (user's already exist)
STARTED SCRIPT
Sophie Hop already exists
Sophie.Hop already exists
Fred Jones account was successfully created
Fred Jones already exists
Fred.Jones already exists
Fred Try account was successfully created
Fred Try already exists
Fred.Try already exists
Ren Jones account was successfully created
Ren Jones already exists
Ren.Jones already exists
Sophie Hop account was successfully created
FINISHED SCRIPT
J Mymryk
Nov 05, 2021Copper Contributor
Hi,
You need to look at your logic a bit closer. You have IF Statements but they are not nested or
are using AND comparison to have both components match. Also you are not throwing a variable that says the user exists to provide a logic check if you actually need to create the account. Right now it runs each if statement then runs the creation statement.
I find it may help sometimes to write down in a document the logic process before tackling the coding.
You need to look at your logic a bit closer. You have IF Statements but they are not nested or
are using AND comparison to have both components match. Also you are not throwing a variable that says the user exists to provide a logic check if you actually need to create the account. Right now it runs each if statement then runs the creation statement.
I find it may help sometimes to write down in a document the logic process before tackling the coding.
- Jord9857Nov 10, 2021Copper ContributorSo what do I need to do/change please?
- J MymrykNov 18, 2021Copper Contributor
I've put some additional comments in your script but as I said before write out the logic on some paper and it will help you write the correct statements. Right now you just have a loop that applies every action to each item that goes through the script. The if statements should have an else statement that creates the list of users you are going to act against or you will just attempt to recreate all users each time. The creation portion of the script should only loop through the users who don't exist properly, not every user.
# Start Commands Write-Host "STARTED SCRIPT`r`n" # Import Active Directory Module For Running AD Cmdlets Import-Module ActiveDirectory # Store The Data From ADUsers.csv in THE $ADUsers variable $filepath = Import-Csv -Path C:\Users\Administrator\Desktop\users1.csv # Loop through each row containing user details in the csv file ForEach ($user in ($filepath)) { # Check if User(s) display name already exists $ADUser = Get-AdUser -Filter {$displayname -eq $User.DisplayName} if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') { Write-Host "$displayname already exists" -ForegroundColor Red } #What are you going to do if the user doesn't exist? (else Statement should be here to put the Incorrects into an array to correct later right?? # Check if User(s) username already exists $ADUser = Get-AdUser -Filter {$SamAccountName -eq $User.SamAccountName} if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') { Write-Host "$SamAccountName already exists" -ForegroundColor Red } #What are you going to do if the username doesn't exist? (else Statement should be here to put the Incorrects into an array to correct later right?? # Should not this section only be done for users who didn't pass the above if statement? otherwise the following commands apply to all users in the file # Read user data from each field in each row and assign the data to a variable as below $displayname = $User.'firstname' + " " + $User.'lastname' $firstname = $User.'firstname' $lastname = $User.'lastname' $jobtitle = $User.'jobtitle' $telephone = $User.'telephone' $department = $User.'department' $UPN = $User.'email' $SamAccountName = $User.'username' $EmailAddress = $User.'email' $Password = $User.'password' $OU = $User.'OU' # Create New AD Users New-ADUser -Name "$displayname" -DisplayName "$displayname" -GivenName "$firstname" -Surname "$lastname" -Title "$jobtitle" -OfficePhone "$telephone" -Department "$department" -UserPrincipalName "$UPN" -SamAccountName "$SamAccountName" -EmailAddress "$EmailAddress" -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $true -PasswordNeverExpires $false # Confirm Commands $? | out-null $ADUser = Get-ADUser -Filter {DisplayName -eq $displayname} if ($ADUser -is 'Microsoft.ActiveDirectory.Management.ADUser') {Write-Host "$displayname account was successfully created" -ForegroundColor Green} else {Write-Host "$displayname account was unsuccessfully created" -ForegroundColor Red} $ErrorActionPreference = 'SilentlyContinue' } # Finish Commands Write-Host "FINISHED SCRIPT`r`n"
- AharonBensadounNov 10, 2021Copper ContributorHi Jord9857,
Are you sur your csv is comma delimited ? Because I tested your script and it's working fine for me.- Jord9857Nov 10, 2021Copper Contributor
Yep, tried both CSV (Comma delimited) and CSV UTF-8 (Comma delimited) but still same issue 😕