使用 powershell 将用户添加到 AD

发布于 2024-12-27 05:21:22 字数 607 浏览 0 评论 0原文

我有一个关于如何使用 powershell 将用户添加到 AD 的问题,我编写了一个小脚本,但当我尝试创建用户时总是收到错误。

$connection= "LDAP://ou=Users, dc="domain", dc="com" 
    $OU = [adsi] $Connection
            $User = $OU.Create("user", "Test Person")
            $User.Put("Firstname", "Test")
            $User.Put("Surname", Person)
            $User.Put("Email", "[email protected]")
            $User.SetInfo()

我认为我的连接字符串是错误的,但我已经尝试了不同的方法,但仍然没有成功。这是我在本地尝试的。需要让它工作,但通常我的 AD 位于不同的服务器上,那么该怎么办?

提前致谢。

I have a question about how to add users to AD using powershell, ive written a small script but i always get an error when i try to create a user.

$connection= "LDAP://ou=Users, dc="domain", dc="com" 
    $OU = [adsi] $Connection
            $User = $OU.Create("user", "Test Person")
            $User.Put("Firstname", "Test")
            $User.Put("Surname", Person)
            $User.Put("Email", "[email protected]")
            $User.SetInfo()

I think my connection string is wrong, but i tried different ways already and still no success. This im trying locally. Need to get it working but then normally my AD is on different server, how to do it then?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新人笑 2025-01-03 05:21:22

尝试一下:

$container = [ADSI] "LDAP://dc.sopragroup.lan/cn=Users,dc=sopragroup,dc=lan"
$UserName = "user"
$User = $container.Create("User", "cn=" + $UserName)
$User.Put("sAMAccountName", $UserName)
$User.Put("givenName", "Test")
$User.Put("sn", "Person")
$User.Put("mail", "[email protected]")
$User.SetInfo()
$User.psbase.InvokeSet('AccountDisabled', $false)
$User.SetInfo()
$User.SetPassword("P@55w0rd")

Give this a try:

$container = [ADSI] "LDAP://dc.sopragroup.lan/cn=Users,dc=sopragroup,dc=lan"
$UserName = "user"
$User = $container.Create("User", "cn=" + $UserName)
$User.Put("sAMAccountName", $UserName)
$User.Put("givenName", "Test")
$User.Put("sn", "Person")
$User.Put("mail", "[email protected]")
$User.SetInfo()
$User.psbase.InvokeSet('AccountDisabled', $false)
$User.SetInfo()
$User.SetPassword("P@55w0rd")
悟红尘 2025-01-03 05:21:22

这是另一个示例(@Andy Arismendi 是第一个),其中包含其他一些详细信息:

  1. 如果您想提供用户和密码(使用与当前用户不同的用户登录服务器),您可以使用 DirectoryEntry< /code> 构造函数
  2. 一个常见的错误是,当您在目录中创建对象时,在目录树中表示该对象的名称是使用以下结构构建的:attribute=value。在 Active-Directory 中,您无法选择架构强加的属性。对于 userinetOrgPerson 来说,它是 CN;对于 organizationalUnit 来说,它是 OU。在您的例子中,对象的名称是CN=Test Person

您将在此处找到 OU 和用户的创建。

Clear-Host
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.234.200:389/dc=dom,dc=fr","[email protected]","admin")

# Create an OU
$Monou = $dn.create("OrganizationalUnit", "ou=Monou")
#$Monou.Description = "Une description"
$Monou.put("Description", "Une description")
$Res = $Monou.Setinfo()


# Create a user
$objUtilisateur = $Monou.create("inetOrgPerson", "cn=Marc Assin")
$objUtilisateur.setinfo()

$objUtilisateur.samaccountname = "Massin"
$objUtilisateur.givenName = "Marc"
$objUtilisateur.sn = "Assin"
#$objUtilisateur.displayName = $objUtilisateur.givenName + " " + $objUtilisateur.sn
$objUtilisateur.userPrincipalName = "[email protected]"

# Pu the state of the account#$objUtilisateur.SetPassword("test.2010")
$objUtilisateur.pwdLastSet = 0
$objUtilisateur.userAccountControl = 544 

# Write the datas of the user
$objUtilisateur.SetInfo()

Here is another example (@Andy Arismendi was first) with some other details:

  1. If you want to give a user and a password (log onto the server with a different user than the current one), you can use the DirectoryEntry constructor
  2. An error that is commonly done is that when you create an object in a directory, the name that represent this object in the directory tree is built with the construction : attribute=value. In Active-Directory you can't choose the the attribute it's imposed by the schema. For a user or an inetOrgPerson it's CN for an organizationalUnit it's OU. In your case the name of the object is CN=Test Person.

You'll find here under the creation of an OU and a user.

Clear-Host
$dn = New-Object System.DirectoryServices.DirectoryEntry ("LDAP://192.168.234.200:389/dc=dom,dc=fr","[email protected]","admin")

# Create an OU
$Monou = $dn.create("OrganizationalUnit", "ou=Monou")
#$Monou.Description = "Une description"
$Monou.put("Description", "Une description")
$Res = $Monou.Setinfo()


# Create a user
$objUtilisateur = $Monou.create("inetOrgPerson", "cn=Marc Assin")
$objUtilisateur.setinfo()

$objUtilisateur.samaccountname = "Massin"
$objUtilisateur.givenName = "Marc"
$objUtilisateur.sn = "Assin"
#$objUtilisateur.displayName = $objUtilisateur.givenName + " " + $objUtilisateur.sn
$objUtilisateur.userPrincipalName = "[email protected]"

# Pu the state of the account#$objUtilisateur.SetPassword("test.2010")
$objUtilisateur.pwdLastSet = 0
$objUtilisateur.userAccountControl = 544 

# Write the datas of the user
$objUtilisateur.SetInfo()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文