2013-06-19 2 views
2

Active Directory에서 컴퓨터를 수집하고이를 List<>으로 반환하는 디렉터리 서비스 방법이 있습니다.C# 디렉터리 서비스 목록에 두 번 추가

코드는 다음과 같습니다 어떤 이유

public static List<string> PCsAndAttributes(string PCName, string AttributeToRead) 
{ 
     List<string> toReturn = new List<string>(); 
     try 
     { 
      string LdapPath = "LDAP://corp.company.com"; 
      DirectoryEntry computer = new DirectoryEntry(LdapPath); 

      DirectorySearcher search = new DirectorySearcher(PCName); 

      string searchAtt = "Name"; 

      search.Filter = "(" + searchAtt + "=" + PCName + ")"; 

      search.PropertiesToLoad.Add(AttributeToRead); 

      SearchResultCollection results = search.FindAll(); 

      foreach (SearchResult result in results) 
      { 
       ResultPropertyCollection PCS = result.Properties; 

       if (!(toReturn.Contains(Convert.ToString(PCS)))) 
       { 
        toReturn.Add(Convert.ToString(PCS[AttributeToRead][0])); 
       } 
      } 

      return toReturn; 
     } 
     catch (Exception err) 
     { 
      toReturn.Add(err.Message); 
      return toReturn; 
     } 
    } 

이 내 트 리뷰에있는 모든 컴퓨터의 두 만드는 것입니다. 99 % 오류가이 단계에 있다고 확신하지만 중복을 제거 할 수 없습니다. 여기

는 트 리뷰 노드 코드 :

private void UpdateLists() 
    { 
     List<string> AdFinds = ProfileCleaner.smallClasses.AdClasses.PCsAndAttributes(txtComputers.Text, "Name"); 
     lblCount.Text = "PC Count: " + AdFinds.Count(); 

     foreach (string PC in AdFinds) 
     { 
      string online = ProfileCleaner.smallClasses.PingIt.Pingit(PC); 

      if (online == "Success") 
      { 
       TreeNode pNode = treePCs.Nodes.Add(PC); 
       pNode.Checked = true; 

       string OS = ProfileCleaner.smallClasses.OsVersion.GetOsVersion.OSVersion(PC); 

       string SubPath = null; 
       if (OS == "6") 
       { 
        SubPath = @"\C$\Users\"; 
       } 
       else 
       { 
        SubPath = @"\C$\Documents and Settings\"; 
       } 

       try 
       { 
        string[] usrs = Directory.GetDirectories(@"\\" + PC + SubPath); 
        foreach (string usr in usrs) 
        { 
         List<string> noAdds = new List<string>(); 
         noAdds.Add("admin"); noAdds.Add("Administrator"); 

          string[] lName = usr.Split('\\'); 
          string user = Convert.ToString(lName[lName.Length - 1]); 

         if (!(noAdds.Contains(user))) 
         { 
         pNode.Nodes.Add(usr); 
         } 
        } 
       } 
       catch (Exception folderErr) 
       { 
       } 
      } 
     } 
    } 

내가 Active Directory에서 모든 기계의 두 무엇입니까 왜 누군가가 말해 줄래? 를 중지하지 않는다

if (!(myList.contains(NewMachine)) { } 

:

I 캡처하고, 아마 그 내 논리를 제거하지만, 같은 일을 시도하기 위해 노력했다.

답변

2

활성 디렉토리에서 컴퓨터는 사용자와 컴퓨터입니다. 검색 문자열은 search.Filter = "(&(" + searchAtt + "=" + PCName + ")(objectclass=computer))";

+0

귀하의 게시물을 읽 자마자 "Duh"순간이었습니다. 그래서, 고마워. –

+0

도움이 될만한 문제가 없습니다. –

0

당신이 피하고 싶은 경우 중복해야한다 :

if (!(toReturn.Contains(Convert.ToString(PCS[AttributeToRead][0])))) 

내가 AD 중복을 반환 이유를 알고에 관심이있을 것입니다. :)

+0

이어야하며 작동하지 않았습니다. 그것이 나왔을 때 바보 같은 실수였습니다. 컴퓨터 조회를 사용자 조회라고 부르기 때문에 FindAll()을 수행했을 때 사용자 계정과 컴퓨터 계정 James McShane이 지적 했었습니다. 나는 광고에서 충분한 시간을 보냈지만 나는 정말로 그것을 알아야만했다. –