[C#][VB.NET]列出網域中所有電腦名稱(包含網域控制站)

  • 10001
  • 0
  • C#
  • 2011-06-28

摘要:列出網域中所有電腦名稱(包含網域控制站)

看過 余小章的 [C#.NET][VB.NET] 如何 存取 Active Directory 帳號資訊 / How to Get Active Directory of Information

直覺想到 那麼要如何讀取 電腦名稱

其實很簡單 利用 DirectoryEntry 的 Children 就可以了

[C#]


Domain domain = Forest.GetCurrentForest().Domains[0];//取得電腦所在網域名稱

DirectoryEntry computers = new DirectoryEntry("LDAP://CN=computers," + GetDomainName(domain.Name), txtUserID.Text, txtPassword.Text);
            
//DC 電腦名稱
foreach(DomainController dc in domain.DomainControllers)
{
    lbComputers.Items.Add(dc.Name.Split('.')[0].ToUpper());
}

//NonDC 電腦名稱
foreach (DirectoryEntry computer in computers.Children)
{
    string[] split = null;
    split =  computer.Name.Split('=');
    lbComputers.Items.Add(split[1]);
}

lbComputers.Sorted = true;

 

[VB .Net]


Dim domain As Domain = Forest.GetCurrentForest().Domains(0) '取得電腦所在網域名稱

Dim computers As New DirectoryEntry("LDAP://CN=computers," + GetDomainName(domain.Name), txtUserID.Text, txtPassword.Text)

'DC 電腦名稱
For Each dc As DomainController In domain.DomainControllers
    lbComputers.Items.Add(dc.Name.Split(".")(0).ToUpper())
Next

'NonDC 電腦名稱
For Each computer As DirectoryEntry In computers.Children
    Dim split As String() = Nothing
    split = computer.Name.Split("=")
    lbComputers.Items.Add(split(1))
Next

lbComputers.Sorted = True

 

CSWindowsApplication1.rar

VBWindowsApplication1.rar