powershell 将LDAP用户添加为非Microsoft Active Directory中安全组的新成员

zvokhttg  于 2023-10-18  发布在  Shell
关注(0)|答案(1)|浏览(189)

有人可以帮助我创建一个Powershell脚本,可以将现有用户添加为目录安全组的新成员吗?我没有Microsoft Active Directory服务器,我使用的是Zentyal Server,据我所知,它使用的是Samba3目录服务。
我能够使用以下Powershell脚本创建此LDAP服务器的新用户帐户,但我不知道如何将用户添加为名为Production的安全组的新成员。
这是用于向LDAP服务器添加新用户的powershell脚本。

  1. # Server credential
  2. $ldapServer = "LDAP://w.x.y.z/DC=acne,DC=local"
  3. $ldapUsername = "[email protected]"
  4. $ldapPassword = "user_password"
  5. # Connect to the LDAP server
  6. $conn = New-Object System.DirectoryServices.DirectoryEntries($ldapServer, $ldapUsername,
  7. $ldapPassword)
  8. $conn.AuthenticationType = [System.DirectoryServices.AuthenticationTypes]::Secure
  9. $properties = @{
  10. "sAMAccountName" = "user"
  11. "userPrincipalName" = "[email protected]"
  12. "userAccountControl" = 66048
  13. "givenName" = "User"
  14. "sn" = "One"
  15. "displayName" = "User One"
  16. }
  17. # Add new user to the LDAP
  18. $cn = "CN=" + $Props\["displayName"\] + ",CN=Users"
  19. $newUser = $Conn.Children.Add($cn, "user")
  20. foreach ($p in $Props.GetEnumerator()) {
  21. $newUser.Properties\[$p.Key\].Value = $p.Value
  22. }
  23. $newUser.CommitChanges()
  24. $newUser.Close()

我尝试使用属性“memberOf”,值为“cn=production,cn=users,dn=acme,dn=com”

l2osamch

l2osamch1#

创建用户后:

  1. $groupDn = "Your Group distinguished name"
  2. $group = [ADSI]"LDAP://$groupDN"
  3. $group.Member.Add("Your User distinguished name") | Out-Null
  4. $group.SetInfo()

您可以通过简单地将Remove方法替换为Add方法来执行相同的操作以删除用户。
请注意,这不适用于集成安全主体。对于这些,只有内置容器中的组可用,并且DN必须替换为SID,SID的字符串与<SID=S-.*>匹配

相关问题