Reputation: 2655
I have a piece of powershell script that loops thru the name of users and adds them to Active Directory into an OU (organizationalUnit), but I have a problme : by some reason I can't add users to that organizationalUnit. I get an error on create user, which says that object is empty, so in other words I think my connection string doenst seems to work , I tried everything and dont know how to solve.
Just a notice when I do the same but for cn it does add users but for OU not ...
Here is the piece of script:
$Connection = "LDAP://ou=SopraUsers,dc=sopragroup,dc=lan"
# Get A Unique Password
[string]$Password = Generate-Password
$username=$Firstname.substring(0,1).toLower() + $Surname.toLower().replace(" ", "")
# Create User in AD
$container =[ADSI] $Connection
$User = $container.Create("User", "cn="+$username)
$User.Put("sAMAccountName", $username)
$User.Put("givenName", $Firstname)
$User.Put("sn", $Surname)
$User.Put("mail", "")
$User.Put("displayName", $Firstname + " "+$Surname)
$User.SetInfo()
$User.PsBase.Invoke("SetPassword", $Password)
$User.PsBase.InvokeSet("AccountDisabled", $false)
$User.pwdLastSet = 0
$User.SetInfo()
I think the problem is in $Connection = "LDAP://ou=SopraUsers,dc=sopragroup,dc=lan"
because if I do $Connection = "LDAP://cn=Users,dc=sopragroup,dc=lan"
Then i do get people added but only for Users.
Here is how my AD looks like, as you can see I want ppl get added to the lowest OU.
Thanks in advance for the help
Upvotes: 0
Views: 1364
Reputation: 72620
Just put this line in comment :
$User.Put("mail", "")
If you don't want to put an email address just remove this line ; on my server it gives a bad attribute syntax.
As far as the connexion string is concern, can you try this connexion string ?
"LDAP://sopragroup.lan/ou=SopraUsers,dc=sopragroup,dc=lan"
You can create a connexion like this :
$Connection = [adsi] "LDAP://sopragroup.lan/ou=SopraUsers,dc=sopragroup,dc=lan"
or, if you want to authenticate :
$Connection New-Object System.DirectoryServices.DirectoryEntry ("LDAP://ServerIP/ou=SopraUsers,dc=sopragroup,dc=lan","[email protected]","pwd")
Upvotes: 1