WinNT提供了大量信息。我需要缩小到机器名称

时间:2011-06-06 13:33:26

标签: vb.net visual-studio-2010

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

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

我无法弄清楚什么会削减这个用户列表,只给我机器名称。

2 个答案:

答案 0 :(得分:2)

您可能会发现here ...查看“枚举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

答案 1 :(得分:1)

我不确定我们的活动目录设置是否有很大差异,但我在控制台应用程序中运行以下代码,它只输出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”的成员