WinNT提供了很多信息.我需要把范围缩小到机器名


Dim de As New System.DirectoryServices.DirectoryEntry()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        de.Path = "WinNT://*****".Replace("*****", ActiveDirectory.Domain.GetCurrentDomain.Name)
        Dim Mystream As Object
        MsgBox("Please choose the place you want the file")
        If savefileDialog1.ShowDialog() = DialogResult.OK Then Mystream = savefileDialog1.FileName
    Dim UserFile As String = savefileDialog1.FileName & ".txt"
    Dim fileExists As Boolean = File.Exists(UserFile)
    Using sw As New StreamWriter(File.Open(UserFile, FileMode.OpenOrCreate))
        For Each d As DirectoryEntry In de.Children()
            sw.WriteLine(d.Name)
        Next
    End Using
End Sub

我正在把大量的条目写到文本文件中。文件的下半部分是我真正需要的。下半部分似乎是域上所有机器名称的列表,前半部分填充了名称或打印机,以及我无法"推入"的其他名称。

我不知道是什么能减少这个用户列表,只给我机器名称。

您可能会在这里找到一些东西。。。查看"枚举OU中的对象">

Public Function EnumerateOU(OuDn As String) As ArrayList
    Dim alObjects As New ArrayList()
    Try
        Dim directoryObject As New DirectoryEntry("LDAP://" + OuDn)
        For Each child As DirectoryEntry In directoryObject.Children
            Dim childPath As String = child.Path.ToString()
            alObjects.Add(childPath.Remove(0, 7))
            'remove the LDAP prefix from the path
            child.Close()
            child.Dispose()
        Next
        directoryObject.Close()
        directoryObject.Dispose()
    Catch e As DirectoryServicesCOMException
        Console.WriteLine("An Error Occurred: " + e.Message.ToString())
    End Try
    Return alObjects
End Function

我不确定我们的active directory设置是否有太大差异,但我在控制台应用程序中运行了以下代码,它只输出AD名称(如预期(:

Module Module1
    Sub Main()
        Using de As New System.DirectoryServices.DirectoryEntry
            de.Path = "WinNT://*****".Replace("*****", System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain.Name)
            For Each d As System.DirectoryServices.DirectoryEntry In de.Children()
                If d.SchemaEntry.Name = "User" Then
                    Console.WriteLine(d.Name)
                End If
            Next
            Console.ReadKey()
        End Using
    End Sub
End Module

编辑:

代码更改为仅输出SchemaType为"User"的成员

最新更新