none
ComputerPrincipal.Save hanging RRS feed

  • Question

  • I need to create a computer in Active Directory and thought I would use System.DirectoryServices.AccountManagement as it was new and the syntax looks cleaner than System.DirectoryServices.

    On my virtual machine network everything seems to be fine, but when I try to run the same code on a real network (virtualised) the call to Save() never returns. The code is as follows in C#

    public ComputerPrincipal CreateNewComputer(string ou, string computerName, string servicePrincipalName)
            {
                using (var principalContext = GetPrincipalContext(ou))
                {
                    var computerPrincipal = new ComputerPrincipal(principalContext, computerName, "", true);

                    computerPrincipal.ServicePrincipalNames.Add("HOST/" + servicePrincipalName);
                    Log.Info("saving");
                    computerPrincipal.SamAccountName = computerName + "$";
                    computerPrincipal.Save();
                    Log.Info("saved");
                    return computerPrincipal;
                }
            }

    In my logs I get to 'saving' but never see 'saved' and I get no exceptions as well. The machine appears in AD with the correct ServicePrincipalName and the modified SamAccountName, BUT it is not enabled.

    Can anybody give me a clue of where to start looking? I have looked in the event log on the Active Directory server, tried creating the machine not enabled. But all have failed to help.

    Thursday, October 27, 2016 4:45 PM

All replies

  • Hi Vin27102016,

    Since ComputerPrincipal Class is inherited from Principal.

    when checking Principal.Save Method from MSDN.

    If the specified principal context differs from the existing context, the principal is moved to the specified context, and the principal object no longer is located in the old location. If the specified context is identical to the existing context, this method saves the updates that were made on the principal object to the store. The context type of the specified context must be the same type as the current context type. This method may not be called by using the computer context type. 

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Kristin Xie Friday, October 28, 2016 3:07 AM
    Friday, October 28, 2016 3:07 AM