Results 1 to 2 of 2

Thread: HOW TO: List Computers on a Network

  1. #1
    Join Date
    Mar 2003
    Posts
    1,700
    Rep Power
    0

    Wink HOW TO: List Computers on a Network

    In addendum to a thread I started earlier, I decided to lookup how one can find the list of computers and domains that exist on the network without using the WMI interface. While the WMI method works, it can take a little while to load.

    Thus, here's a much faster method to listing computers and domains in your network.

    1. Right-Click on your project name in the Solution Explorer, and select "Add Reference..." from the pop up menu.

    2. In the Dialog that appears, under the .NET tab, search for System.DirectoryServices. Select it and click on "Ok".

    3. Drag 2 combobox objects onto your form.

    4. In your code, add the following line above your class definition:

      Code:
      Imports System.DirectoryServices
    5. Then in the body of your program, add the following code to the Form_Load event handler (i.e. a subroutine that handles the MyBase.Load event):


    Code:
    'Determine the list of domains on the network - trusted or not
    Dim parent As New DirectoryEntry("WinNT:")
    Dim child As DirectoryEntry
    Try
        For Each child In parent.Children
            'If the child element is a domain, add it to the combo box list
            If child.SchemaClassName =  "Domain" Then
                ComboBox1.Items.Add(child.Name)
            End If
        Next
    
        'Now list all of the computers in the current domain. Notice we used our
        'own domain name to filter the results, using the 'Environment' class object
        Dim subParent As New DirectoryEntry("WinNT://" & Environment.UserDomainName)
        For Each child In subParent.Children
            'If the child object is a computername, add it to the second
            'combo box
            If child.SchemaClassName = "Computer" Then
                ComboBox2.Items.Add(child.Name)
            End If
        Next
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Query Failed")
    End Try
    The code is lightning fast becasue it's success doesn't rely on executing a WMI query on the domain.

    • Tip #1: This code is specific to NT Domains. If you want to use it for an LDAP based domain or network, (eg. ActiveDirectory, or Linux LDAP), simply substitute the text value: "WinNT://" with "LDAP://" and it will work just the same.

    • Tip #2: If you like, you could automate the listing of the Computer Names in ComboBox #2 when the user changes the domain name in combo box #1. Simply attach the second part of the code to a subroutine that handles the ComboBox.SelectedIndexChanged event of ComboBox1, then Substitute Environment.UserDomainName with ComboBox1.Text. Remember to do a ComboBox2.Items.Clear() before the rest of the code!

    So for those of you implemented the SQLDMO query that I posted on here some time ago, you can provide a Try... Catch block in case that code doesn't work, and in the Catch section of the block, just implement the above code so that at least the end user can see a list of machines with which they can select an appropriate machine.

    Enjoy.

  2. #2
    Join Date
    Mar 2003
    Posts
    1,700
    Rep Power
    0

    Exclamation To get Network Users

    Addendum

    To list the users in the network domain using the same examples mentioned above, simply change the test string used in the child.SchemaClassName to "User" instead of "Computer" to get a list of Network users.

    Erratum

    Actually, you don't need to change the text value from "WinNT://" to "LDAP://" for ActiveDirectory domains. "WinNT://" will work just fine for either. LDAP domains are still WinNT domains so long as they're being facilitated by Win2K or Win2K3.
    Last edited by Xenocrates; Jul 18, 2006 at 12:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •