使用powershell将用户导入到OU中的OU
问题描述:
我有一段PowerShell脚本循环通过用户名并将它们添加到活动目录中进入OU(organizationalUnit),但我有一个问题:由于某种原因,我可以'将用户添加到该组织单位。我在创建用户时遇到了一个错误,该用户说该对象是空的,所以换句话说,我认为我的连接字符串似乎可以正常工作,我尝试了所有内容并且不知道如何解决。使用powershell将用户导入到OU中的OU
只是一个通知,当我做相同的,但对于CN它确实增加了用户但不OU ...
下面是一段脚本:
$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()
我认为这个问题是在$Connection = "LDAP://ou=SopraUsers,dc=sopragroup,dc=lan"
因为如果我这样做$Connection = "LDAP://cn=Users,dc=sopragroup,dc=lan"
然后我确实让人们加入,但只为用户。
下面是我的广告看起来像,你可以看到我想要ppl被添加到最低的OU。
在此先感谢您的帮助
答
只是把这个行注释:
$User.Put("mail", "")
如果你不希望把一个电子邮件地址只是删除这条线;在我的服务器上,它提供了一个错误的属性语法
就连接字符串而言,你可以试试这个连接字符串吗?
"LDAP://sopragroup.lan/ou=SopraUsers,dc=sopragroup,dc=lan"
您可以创建这样一个联接:
$Connection = [adsi] "LDAP://sopragroup.lan/ou=SopraUsers,dc=sopragroup,dc=lan"
,或者,如果要进行身份验证:
$Connection New-Object System.DirectoryServices.DirectoryEntry ("LDAP://ServerIP/ou=SopraUsers,dc=sopragroup,dc=lan","[email protected]","pwd")
你能给出确切的错误消息吗? – JPBlanc 2012-01-18 04:14:21