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...
AharonBensadoun
Nov 02, 2021Copper Contributor
Hi,
Your script is correct but you are miss something:
In your csv file you doesn't have DisplayName or SamAccountName value, so you can't check if user exist or not , see line 15 of your script:
$ADUser = Get-AdUser -Filter {$displayname -eq $User.DisplayName}
So you have the choice, or you adding this columns in the csv with the correct value or you check if user exist with other parameters, for example firstname or lastname combined together.
Hope this help
- Jord9857Nov 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=LocalThis 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 SCRIPTThis 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 MymrykNov 05, 2021Copper ContributorHi,
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?