2014-01-30 26 views
0

我正在從SSIS中的C#(3.5框架)中的腳本任務獲取Active Directory中的值。完成這個代碼似乎工作正常。我使用一個嵌套循環,外部循環拉動非多值的值,並用存儲過程將它們插入表中,然後將該行的PK作爲第二個循環中的FK傳回給第二個循環存儲過程將用戶所屬的所有組插入到第二個表中。Active Directory描述字段值沒有顯示

我的問題是我無法從我認爲應該包含每個組的描述的Description字段獲取任何值。如果我通過它並檢查描述對象的計數,它顯示0.使用相同的代碼來檢查Memberof的計數,我得到用戶所屬的組數。我不明白的另一部分是我從外部循環中的Description字段獲取值,但它更接近於用戶記錄的評論,而不是單個組的描述。它好像我的Description沒有多值。但是其他人可以通過AD門戶確認Description字段具有描述每個組的內容的值。我很難過。見下面的代碼。

DirectoryEntry entry = new DirectoryEntry("LDAP:[my address]"); 
DirectorySearcher Dsearch = new DirectorySearcher(entry); 
Dsearch.Filter = "(&(objectClass=User))"; 

foreach (SearchResult sResultSet in Dsearch.FindAll()) 
{ 
    (code continues from original post) 

    using (SqlConnection dataConnection = new SqlConnection([mysqlconnection])) 
    { 
     using (SqlCommand dataCommand = dataConnection.CreateCommand()) 
     { 
      dataCommand.CommandText = "ActiveDirectory.InsertParentRecords"; 
      dataCommand.CommandType = CommandType.StoredProcedure; 

      dataCommand.Parameters.AddWithValue("@PackageLogId", Dts.Variables["PackageLogId"].Value.ToString()); 
      dataCommand.Parameters.AddWithValue("@cn", GetProperty(sResultSet, "cn")); 
      dataCommand.Parameters.AddWithValue("@givenName", GetProperty(sResultSet, "givenName")); 
      dataCommand.Parameters.AddWithValue("@initials", GetProperty(sResultSet, "initials")); 
      dataCommand.Parameters.AddWithValue("@sn", GetProperty(sResultSet, "sn")); 

      dataCommand.Parameters.AddWithValue("@mail", GetProperty(sResultSet, "mail")); 
      dataCommand.Parameters.AddWithValue("@Name", GetProperty(sResultSet, "Name")); 
      dataCommand.Parameters.AddWithValue("@middleName", GetProperty(sResultSet, "middleName")); 
      dataCommand.Parameters.AddWithValue("@title", GetProperty(sResultSet, "title")); 
      dataCommand.Parameters.AddWithValue("@employeeID", GetProperty(sResultSet, "employeeID")); 

      dataCommand.Parameters.AddWithValue("@employeeNumber", GetProperty(sResultSet, "employeeNumber")); 
      dataCommand.Parameters.AddWithValue("@objectSid", ConvertSidToString((byte[])sResultSet.Properties["objectSid"][0])); 
      dataCommand.Parameters.AddWithValue("@userAccountControl", tempuserAccountControl); 
      dataCommand.Parameters.AddWithValue("@whenCreated", GetProperty(sResultSet, "whenCreated")); 
      dataCommand.Parameters.AddWithValue("@distinguishedName", GetProperty(sResultSet, "distinguishedName")); 

      dataCommand.Parameters.AddWithValue("@badPasswordTime", Convert.ToString(badPasswordTime)); //Issues!! 
      dataCommand.Parameters.AddWithValue("@badPwdCount", GetProperty(sResultSet, "badPwdCount")); 
      dataCommand.Parameters.AddWithValue("@memberof", GetProperty(sResultSet, "memberof")); 
      dataCommand.Parameters.AddWithValue("@samaccountname", GetProperty(sResultSet, "samaccountname")); 
      dataCommand.Parameters.AddWithValue("@Description", GetProperty(sResultSet, "Description")); 

      dataCommand.Parameters.AddWithValue("@maxPwdAge", GetProperty(sResultSet, "maxPwdAge")); //Issues!!        
      dataCommand.Parameters.AddWithValue("@pwdLastSet", pwdLastSet); //Issues!! 
      dataCommand.Parameters.AddWithValue("@LockOutTime", Convert.ToString(LockOutTime));  //Issues!! 

      if (disabled == false) //Issues!! 
       dataCommand.Parameters.AddWithValue("@Acctdisabled", '0'); 
      else 
       dataCommand.Parameters.AddWithValue("@Acctdisabled", '1'); 

      dataCommand.Parameters.AddWithValue("@displayname", GetProperty(sResultSet, "displayname")); 

      dataCommand.Parameters.AddWithValue("@twofactor", twofactor); 
      dataCommand.Parameters.AddWithValue("@skiprecord", skiprecord); 

      dataCommand.Parameters.Add("@DetailID", SqlDbType.Int); 
      dataCommand.Parameters["@DetailID"].Direction = ParameterDirection.Output; 

      dataConnection.Open(); 
      dataCommand.ExecuteScalar(); 
      dataConnection.Close(); 

      Counter++; 
      DetailID = (int)dataCommand.Parameters["@DetailID"].Value; 
     } 
    } 

    using (SqlConnection dataConnection = new SqlConnection[mysqlconnection])) 
    { 
     using (SqlCommand dataCommand = dataConnection.CreateCommand()) 
     { 
      dataConnection.Open(); 

      int groupCount = sResultSet.Properties["memberOf"].Count; 
      int DescriptionCount = sResultSet.Properties["Description"].Count; 

      for (int counter = 0; counter < groupCount; counter++) 
      { 
       dataCommand.CommandText = "ActiveDirectory.InsertMemberOf"; 
       dataCommand.CommandType = CommandType.StoredProcedure; 

       dataCommand.Parameters.Clear(); 
       dataCommand.Parameters.AddWithValue("@PackageLogId", Dts.Variables["PackageLogId"].Value.ToString()); 
       dataCommand.Parameters.AddWithValue("@DetailID", DetailID); 

       if (sResultSet.Properties.Contains("Description")) 
       { 
        dataCommand.Parameters.AddWithValue("@Group", sResultSet.Properties["Description"][counter].ToString()); 
       } 
       else 
       { 
        dataCommand.Parameters.AddWithValue("@Group", "n/a"); 
       }        

       dataCommand.Parameters.AddWithValue("@memberOf", sResultSet.Properties["memberOf"][counter]); 

       dataCommand.ExecuteScalar(); 
       InnerCounter++; 
      } 
     } //End of DataCommand 
    } //End of Data Connection 


    Debug.WriteLine(GetProperty(sResultSet, "displayname") + " " + Counter + ", " + InnerCounter + ", " + GetProperty(sResultSet, "userAccountControl")); 
    InnerCounter = 0;       
    } //End of For Each Loop 
+0

添加代碼以顯示如何在編輯中填充sResultSet,@ billinkc –

+0

您可以使用ADSI編輯http://technet.microsoft.com/en-us/library/cc773354(v=ws.10).aspx和核實自己在這個領域是什麼? –

+0

我還沒有嘗試過(不再工作了),但我確實有人通過AD門戶驗證說明是每組名稱 –

回答

1

一個LDAP搜索查詢是有點像SQL查詢,我知道這不是明確記載,但你最好聲明要搜索將retrived屬性。你可以嘗試添加

DirectorySearcher Dsearch = new DirectorySearcher(entry); 
... 
Dsearch.PropertiesToLoad.Add("description"); 
... 
Dsearch.Filter = "(&(objectClass=User))"; 

在我的代碼中,我真的添加了所有我需要的屬性。我同意大多數時間都沒有這種情況,但大多數情況下都是如此。