I am trying to add a user to an LDAP server. I am getting the next error:
我正在尝试将用户添加到LDAP服务器。我收到下一个错误:
Exception calling "SendRequest" with "1" argument(s): "The type is not defined."
At line:11 char:1
+ $response = $connection.SendRequest($request)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DirectoryOperationException
This is my code:
这是我的代码:
$credential = New-Object -TypeName System.Net.NetworkCredential -ArgumentList ($ldapUser, $ldapPass)
$directoryIdentifier = New-Object -TypeName System.DirectoryServices.Protocols.LdapDirectoryIdentifier -ArgumentList ($ldapServer, $ldapPort)
$connection = New-Object -TypeName System.DirectoryServices.Protocols.LdapConnection -ArgumentList ($directoryIdentifier, $credential, [DirectoryServices.Protocols.AuthType]::Basic)
$connection.SessionOptions.ProtocolVersion = 3
$connection.Bind()
$request = New-Object -TypeName System.DirectoryServices.Protocols.AddRequest
$request.DistinguishedName = "uid=Test_Thomas,ou=Users,ou=DE,ou=saria,dc=ext,dc=saria,dc=com"
$request.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "objectclass", @("top","organizationalPerson","person","inetorgperson"))) | Out-Null
$request.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "cn", $ExistingFrontlineWorkers[1].cn)) | Out-Null
$request.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "sn", $ExistingFrontlineWorkers[1].sn)) | Out-Null
$request.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "mail", $ExistingFrontlineWorkers[1].mail)) | Out-Null
$request.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "uid", "Test_Thomas")) | Out-Null
$request.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "uidNumber", "1000")) | Out-Null
$request.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "gidNumber", "1000")) | Out-Null
$request.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "homeDirectory", "")) | Out-Null
$response = $connection.SendRequest($request)
There isn´t a problem with the connection because I can search.
连接没有问题,因为我可以搜索。
更多回答
Is it undefined LDAP or ActiveDirectory LDAP? Which version of PS you use?
是未定义的ldap还是ActiveDirectory ldap?您使用的是哪个版本的PS?
OpenLDAP, PS version 5.1.
OpenLDAP,PS 5.1版。
优秀答案推荐
You have DirectoryOperationException
meaning that LDAP service (server) is unwilling to perform this request.
您有DirectoryOperationException,这意味着LDAP服务(服务器)不愿意执行此请求。
For example, if your LDAP service is ActiveDirectory, you MUST create user with userAccountControl
disabled flag (or to set password instead), you MUST set user's distinguished name as CN=Test_Thomas instead
of uid=Test_Thomas
, etc...
例如,如果您的LDAP服务是ActiveDirectory,则必须使用userAccount Control禁用标志创建用户(或者改为设置密码),您必须将用户的可分辨名称设置为cn=Test_Thomas,而不是uid=Test_Thomas,依此类推...
If you use PS5+, you can use ::new()
instead of new-object
like this.
如果您使用PS5+,您可以使用::new()来代替new-Object,如下所示。
Warning: this code is not tested.
警告:此代码未经过测试。
$request = [System.DirectoryServices.Protocols.AddRequest]::new(
'CN=username,OU=Users,DC=contoso,DC=com',
@(
[System.DirectoryServices.Protocols.DirectoryAttribute]::new('samaccountname', 'username')
# ref https://learn.microsoft.com/en-us/troubleshoot/windows-server/identity/useraccountcontrol-manipulate-account-properties
# Create account disabled as it has no password yet
[System.DirectoryServices.Protocols.DirectoryAttribute]::new('userAccountControl', 514)
[System.DirectoryServices.Protocols.DirectoryAttribute]::new('userPrincipalName', '[email protected]')
[System.DirectoryServices.Protocols.DirectoryAttribute]::new('displayName', 'username')
[System.DirectoryServices.Protocols.DirectoryAttribute]::new('objectClass', @('top', 'person', 'organizationalPerson', 'user'))
[System.DirectoryServices.Protocols.DirectoryAttribute]::new('objectCategory', 'CN=Person,CN=Schema,CN=Configuration,DC=contoso,DC=com')
[System.DirectoryServices.Protocols.DirectoryAttribute]::new('distinguishedName', 'CN=username,OU=Users,DC=contoso,DC=com')
[System.DirectoryServices.Protocols.DirectoryAttribute]::new('instanceType', 4)
)
)
Read more or search examples
阅读更多或搜索示例
http://pig.made-it.com/pig-adusers.html
Http://pig.made-it.com/pig-adusers.html
https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adod/1fd2c193-fc70-4f90-a8c8-f1ca2de4e5ac
Https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adod/1fd2c193-fc70-4f90-a8c8-f1ca2de4e5ac
https://gist.github.com/leosouzadias/0f5acd0b70e86f811e25a8fd327db7dc
https://gist.github.com/leosouzadias/0f5acd0b70e86f811e25a8fd327db7dc
https://github.com/zorn96/ms_active_directory/blob/main/ms_active_directory/core/ad_session.py#L338
Https://github.com/zorn96/ms_active_directory/blob/main/ms_active_directory/core/ad_session.py#L338
更多回答
Thanks for the comment, I will try that next week.
谢谢你的评论,我下周会试一试。
我是一名优秀的程序员,十分优秀!