Hi,
The following is a script I’m trying to put together that
takes in a list of users from a database file and adds them as users into the active directory. Also omits duplicates. I’ll comment where I know issues reside, I’m hoping you’ll be kind enough to help me get it to work since PS errors don’t help me much I’m a noob.
Import-Module ActiveDirectory
#EDIT PATH SO IT POINTS TO DB FILE /
#MONDAY’S CSV
$newUserList = Import-Csv C:\Users\Administrator\Desktop\dbs\Monday.csv
ForEach ($item in $newUserList){
$fname = $($item.first_name)
$lname = $($item.last_name)
$phone = $($item.phone1)
#I know everything works up to this point.
$username=$fname+$lname.substring(0,1)
Puts Domain name into a Placeholder.
$domain=‘@csilab.local’
Build the User Principal Name Username with Domain added to it
$UPN=$username+$domain
Create the Displayname
$Name=$fname+” “+$lname
Create User in Active Directory FIRST issue resides here, PS doesn’t understand the following cmdlets, it misinterprets them all as objects. I assume none of it works but I’m especially unsure of the TelephoneNumber as I just guessed on that one:
$newusers1 = (NEW-QADUSER –FirstName $fname –Lastname $lname -Telephonenumber $phone –Name $DisplayName $Name –SamAccountName $username –UserPassword ‘1NewPassword’ –UserPrincipalName $UPN –Name $Name –ParentContainer ‘csilab.local’)
}
#THE FOLLOWING CODE TAKES USERS IN newusers1 VAR AND PUTS THEM IN AD ACTIVE USERS GROUP. I haven’t tested this as script isn’t even making accounts yet. However I know this code section works.
get-AdUser $newusers1 | Move-ADObject -TargetPath ‘OU=Active Users, dc=csilab, dc=local’
Add-ADGroupMember -Identity “Active Users Security” -Members $newusers1
#REMOVES ANY DUPLICATES IN AD THAT GET PAST THE SORTING CODE IN DB FILE IMPORT SECTION. I want it to both confirm same name AND phone number since it’s unlikely you’ll have two people with both matching. Whereas two people with the same name is possible.
(get-AdGroupMember “Active Users Security”) | Remove-ADUser -Identity ‘SamAccountName’ ‘SamTelephoneNumber’ IncludeEqual -confirm:$false
#Error checking component, ignore if it doesn’t work, even with a variable I couldn’t get this to work
if($error -eq 0)
{
write-host “Thank you, the users have been added to the directory successfully.”
$b1 = Read-Host (‘dddd’)
}
if($error -eq 1)
{
write-host “UNEXPECTED ERROR:” -ForegroundColor Red
$error[0]
$b1 = Read-Host (‘dddd’)
}
#Thank you for your time… I know this is a mess.