1

私は悪いディレクトリの日を過ごしています。:)

誰かがこれの何が悪いのか教えてもらえますか?

groupName = "Monkey";
...
using (DirectoryEntry directoryEntryObject = new DirectoryEntry("WinNT://" + Environment.MachineName, "", "", AuthenticationTypes.Secure))
{
     using (DirectoryEntry group = directoryEntryObject.Children.Add("CN=" + groupName.Trim(), "group"))
      {
            group.Properties["sAMAccountName"].Value = groupName;
            group.CommitChanges();
       }
}

私がやろうとしているのは、ローカルアカウントを作成することです。このコードをそのまま試すと、samaccountname プロパティを設定しようとするとクラッシュします。

System.Runtime.InteropServices.COMException occurred
  Message="The directory property cannot be found in the cache.\r\n"
  Source="Active Directory"
  ErrorCode=-2147463153
  StackTrace:
       at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.PutEx(Int32 lnControlCode, String bstrName, Object vProp)
  InnerException:

その行をコメントアウトすると、コミット時に次のようにクラッシュします。

System.Runtime.InteropServices.COMException occurred
  Message="The specified username is invalid. (Exception from HRESULT: 0x8007089A)"
  Source="System.DirectoryServices"
  ErrorCode=-2147022694
  StackTrace:
       at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.SetInfo()
  InnerException: 

ソースについてどう考えるべきかわかりません。W2003 ドメインの Vista を使用していますが、Active Directory グループではなく、ローカル グループを作成しようとしています。

何か案は?私はおそらく明らかな何かを見逃しました。GroupPricipal.Save メソッドを使用してユーザーを作成できるので、権限の問題ではありません。

4

1 に答える 1

3

このコードを試してみてください、私はそれがトリックをするだろうとかなり確信しています;)

using System;
using System.DirectoryServices;

class Class1
{
    static void Main(string[] args)
    {
    try
        {
     DirectoryEntry AD = new DirectoryEntry("WinNT://" + 
                         Environment.MachineName + ",computer");
     DirectoryEntry NewUser = AD.Children.Add("TestUser1", "user");
     NewUser.Invoke("SetPassword", new object[] {"#12345Abc"});
     NewUser.Invoke("Put", new object[] {"Description", "Test User from .NET"});
     NewUser.CommitChanges();
     DirectoryEntry grp;

     grp = AD.Children.Find("Guests", "group");
     if (grp != null) {grp.Invoke("Add", new object[] {NewUser.Path.ToString()});}
     Console.WriteLine("Account Created Successfully");
     Console.ReadLine();
    }
    catch (Exception ex)
    {
     Console.WriteLine(ex.Message);
     Console.ReadLine();

    }
    }
}
于 2009-09-04T02:14:04.573 に答える