使用Powershell和凭据查询AD LDS



我可以使用LDP从Windows服务器连接到我的AD LDS实例,但我很难使用PowerShell从同一服务器连接/绑定和查询我的AD LDS实例。我找不出正确的PowerShell语法。

以下是适用于LDP的连接参数/步骤:

服务器=idm.mydomain.com

端口=636

选中SSL复选框

连接到idm.mydomain.com后,转到绑定

User=CN=canvas_service,OU=Users,OU=Infrastructure Support,DC=idm,DC=mydomain,DC=com

密码=MyPassWord

绑定类型=简单绑定

以下是我在PowerSHell中尝试过的

Import-Module ActiveDirectory

##############################################################################################
# Username, Password of an admin account for the AD LDS and the location of the AD LDS
$credUsername = 'CN=canvas_service,OU=Users,OU=Infrastructure Support,DC=idm,DC=mydomain,DC=com'
$credPassword = 'MyPassWord'
$server = 'idm.mydomain.com:636'
$userName = '*'
##############################################################################################

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList `
@($credUsername,(ConvertTo-SecureString -String $credPassword -AsPlainText -Force))
$user = Get-ADUser -Filter {cn -eq $userName} -SearchBase "OU=Users,OU=Infrastructure Support,DC=idm,DC=mydomain,DC=com" -server $server -Credential $cred

结果Get-ADUser:无法联系服务器。

我在网上找不到任何包含凭据和SSL的PowerShell示例,这些示例可以为我指明正确的方向。非常感谢您的帮助。

在我的评论中,Active Directory模块使用Active Directory Web服务作为协议,不可能将LDAP over SSL(简称LDAPS(与此模块一起使用。您需要使用adsi进行绑定,使用adsisearcher进行查询。

有一段时间没有这样做了,所以下面的代码可能不起作用,但希望能帮助你步入正轨。

$server = "idm.mydomain.com"
$port = 636
$ou = "OU=Users,OU=Infrastructure Support,DC=idm,DC=mydomain,DC=com"
$toSearch = "canvas_service"
# Below might require you to add `None` or `0` to the constructor for SimpleBind
# Beginning with .NET Framework 2.0, the default value is `Secure` or `1`.
$adsi = [adsi]::new("LDAP://${server}:${port}", "myUser", "myPasswordAsPlainText!!")
$searcher = [adsisearcher]::new($adsi)
$searcher.PropertiesToLoad.AddRange(@("samAccountName", "cn", "distinguishedName"))
$searcher.SearchRoot = $ou
$searcher.SearchScope = "SubTree"
$searcher.Filter = "(&(cn=$toSearch)(objectClass=user))"
$searcher.FindOne()

最新更新