Move Object to an OU in Active Directory

丶灬走出姿态 提交于 2020-01-17 12:39:30

问题


I want to move a computer object to another OU I am connected to another domain and I always getting an exception of type ComException "A referral was returned from the server" and the object never move!

        try
        {
            //I get the exception here
            computerObject.MoveTo(new DirectoryEntry("LDAP://OU=someOU,OU=parentOU,DC=test,DC=com"));
            computerObject.CommitChanges();
        }
        catch (InvalidOperationException inOp)
        {
            //log 
        }
        catch (COMException comEx)
        {
            //log 
        }

        //joinPath.Close();
        finally
        {
            computerObject.Close();
        }

for troubleshooting I changed the code a little bit but again it doesn't work.

computerObject.MoveTo(new DirectoryEntry("LDAP://OU=someOU,OU=parentOU,DC=test,DC=com"),
                      "admin@test.com","somepassowrd",AuthenticationTypes.Secure));

the new exception is of type ComException "Logon failure: unknown user name or bad password." I checked that there exists an OU in the active directory and I have enough permissions.

I followed Microsoft docs here https://msdn.microsoft.com/en-us/library/ms180856(v=vs.90).aspx and many stackoverflow questions.

Update: I am running my application in one domain and making changes in another domain, it might be the cause of the problem


回答1:


I published my code in the same domain and it worked just fine




回答2:


You have an extra parenthesis in this method:

computerObject.MoveTo(new DirectoryEntry("LDAP://OU=someOU,OU=parentOU,DC=test,DC=com"),"admin@test.com","somepassowrd",AuthenticationTypes.Secure));

it should be

computerObject.MoveTo(new DirectoryEntry("LDAP://OU=someOU,OU=parentOU,DC=test,DC=com","admin@test.com", "somepassowrd", AuthenticationTypes.Secure));

This would certainly explain the exception, I'm surprised you debugger didn't catch it, unless you are writing it freehand, or maybe you mis-typed it into your question?



来源:https://stackoverflow.com/questions/32345306/move-object-to-an-ou-in-active-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!