ไม่สามารถรับแอตทริบิวต์โฆษณาโดยใช้ DirectoryEntry (แอตทริบิวต์ LAPS)

ฉันประสบปัญหานี้ซึ่งฉันไม่สามารถเรียกแอตทริบิวต์ AD ผ่าน DirectoryEntry ได้ ฉันสามารถรับมันผ่าน DirectorySearcher แต่ฉันไม่สามารถรับหรือตั้งค่าผ่าน DirectoryEntry ได้

คุณลักษณะที่ต้องการคือ ms-Mcs-AdmPwdExpirationTime ซึ่งมี NT TimeStamp ฉันได้อ่านและเขียนลงในคุณลักษณะนี้แล้ว

DirectoryEntry ข้อผิดพลาด C# ในคอนโซล

ข้อผิดพลาด HRESULT E_FAIL ได้รับการส่งคืนจากการเรียกไปยังคอมโพเนนต์ COM

ฉันได้ลองใช้สิ่งต่อไปนี้แล้ว แต่ยังไม่สามารถเรียกข้อมูลแอตทริบิวต์ได้

RefreshCache (string[] propertyNames);

แก้ไข:

ComputerPrincipal comp = ComputerPrincipal.FindByIdentity(ctx, MachineName);
DirectoryEntry de = (DirectoryEntry)comp.GetUnderlyingObject();
if (de.Properties.Contains("ms-Mcs-AdmPwd") == true)
{
    string Password = (String)de.Properties["ms-Mcs-AdmPwd"][0];
    Password_Input.Text = Password;
    DateTime NTTime = DateTime.FromFileTime(ConvertLargeIntegerToLong(de.Properties["ms-Mcs-AdmPwdExpirationTime"].Value));
    PasswordExpiry_Value.Text = NTTime.ToString("dd/MM/yyyy hh:mm:ss");
    Console.WriteLine();
}
else
{
    Password_Input.Text = "Password not set by LAPS";
}
// down the bottom of the .cs
private static long ConvertLargeIntegerToLong(object largeInteger)
{
    var type = largeInteger.GetType();
    var highPart = Convert.ToInt32(type.InvokeMember("HighPart", BindingFlags.GetProperty, null, largeInteger, null));
    var lowPart = Convert.ToInt32(type.InvokeMember("LowPart", BindingFlags.GetProperty, null, largeInteger, null));
    return (long)highPart << 32 | (uint)lowPart;
}

person FingerlessGloves    schedule 03.12.2018    source แหล่งที่มา
comment
คุณกำลังพยายามตั้งค่าคุณลักษณะหรือเพียงแค่อ่านมัน? เราจะเห็นรหัสที่เกิดข้อผิดพลาดนี้ได้หรือไม่?   -  person Shovers_    schedule 03.12.2018
comment
ฉันพบวิธีอ่าน แต่จะมีประโยชน์ถ้าฉันสามารถตั้งค่าได้ ดูโพสต์ไปยังรหัสที่อัปเดต   -  person FingerlessGloves    schedule 03.12.2018


คำตอบ (1)


สำหรับการตั้งค่าคุณสมบัติในอดีต ฉันเคยใช้สิ่งนี้กับรายการไดเร็กทอรี

Path คือเส้นทาง LDAP แบบเต็มไปยังออบเจ็กต์ แต่คุณสามารถแทนที่ de ในตัวอย่างด้านบนได้ หวังว่านั่นจะเพียงพอที่จะแก้ไขปัญหาของคุณหรืออย่างน้อยก็ชี้แนะทิศทางให้คุณ

นอกจากนี้ยังมีคำตอบอื่น ๆ ที่นี่ ว่าทำไมคุณถึงได้รับข้อผิดพลาดนั้น

และ ที่นี่

 public Boolean set_AD_property(string attribute_, string new_value)
    {
        this.AD_object = new DirectoryEntry(this.path_);
        this.AD_object.Properties[attribute_].Value = new_value;
        try
        {
            this.AD_object.CommitChanges();
            this.AD_object.Close();
            return true;
        }
        catch (System.Exception)
        {
            return false;
        }
    }

และสำหรับการอ่าน:

  public object get_AD_property(string attribute_)
    {
        try
        {
            using (this.AD_object = new DirectoryEntry(this.path_))
            {
                return this.AD_object.Properties[attribute_].Value;
            }
        }
        catch (ArgumentNullException x)
        {
            return new ArgumentNullException(x.Message, x);
        }
    }

แม้ว่าสิ่งนี้จะไม่ได้ผลกับคุณสมบัติที่ซับซ้อนมากขึ้นเช่น "members" หรือ "memberOf"

person Shovers_    schedule 03.12.2018