2016-05-31 78 views
1

我正在尋找一種使用PowerShell修改非Active Directory LDAP對象的方法。我發現許多腳本聯機訪問LDAP對象信息,但沒有顯示如何修改它們。下面是我通過結合我在網上找到的各種腳本得到的最接近的結果。我無法越過「$ c.Bind()」行,因爲我總是收到「LDAP服務器不可用」錯誤。我知道服務器名稱是正確的,它已啓動並正在運行。使用Powershell修改非AD LDAP對象

任何人有任何想法?

[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols") 
[System.Reflection.Assembly]::LoadWithPartialName("System.Net") 

$credentials = new-object System.Net.NetworkCredential("cn=adminID,o=edu","password") 
$NetWareServer=New-Object System.DirectoryServices.Protocols.LdapDirectoryIdentifier("LDAP://ldapserver.system.edu:636") 
$c = New-Object System.DirectoryServices.Protocols.LdapConnection($NetWareServer, $credentials) 

$c.SessionOptions.SecureSocketLayer = $true; 
$c.SessionOptions.ProtocolVersion = 3 
$c.AuthType = [System.DirectoryServices.Protocols.AuthType]::Basic 

$c.Bind() 

$r = (new-object "System.DirectoryServices.Protocols.ModifyRequest") 
$r.DistinguishedName = "uid=testID,ou=test,o=edu"; 

$a = New-Object "System.DirectoryServices.Protocols.DirectoryAttributeModification" 
$a.Name = "description" 
$a.Operation = [System.DirectoryServices.Protocols.DirectoryAttributeOperation]::Add 
$a.Add("testdescription") 

$r.Modifications.Add($a) 

$re = $c.SendRequest($r); 

if ($re.ResultCode -ne System.directoryServices.Protocols.ResultCode]::Success) 
{ 
    write-host "Failed!" 
    write-host ("ResultCode: " + $re.ResultCode) 
    write-host ("Message: " + $re.ErrorMessage) 
} 
+1

難道用'ldapserver.system.edu提供的證書:636'不被信任? –

+0

它可以。但是我對證書知之甚少,不知道如何去測試。 – David

+0

我有這個問題,它通過採取AD根證書並將其添加到我的受信任的根機構來解決。 –

回答

0

嘗試使用uid而不是cn作爲憑證。嘗試連接時不使用SSL。驗證端口和用戶信息是否正確,以及服務器和該端口是否可從您的位置訪問。

這是我做的:

$c = New-Object -TypeName System.DirectoryServices.Protocols.LdapConnection -ArgumentList "ldapserver.system.edu:636" 
$c.SessionOptions.SecureSocketLayer = $true; 
$c.SessionOptions.ProtocolVersion = 3 
$c.AuthType = [System.DirectoryServices.Protocols.AuthType]::Basic 
if ([string]::IsNullOrWhiteSpace($ConnectWithUser)) 
{ 
    $ConnectWithUser = Read-Host -Prompt "User:" 
} 
if ([string]::IsNullOrWhiteSpace($ConnectWithPassword)) 
{ 
    $ConnectWithPassword = Read-Host -Prompt "Password:" -AsSecureString 
} 
$ConnectWithUser = "uid="+$ConnectWithUser+",OU=admins,O=edu" 

$credentials = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $ConnectWithUser,$ConnectWithPassword 
$c.Bind($credentials) 

好運