Title:  Using WMI to retrieve Information - Build Simple Network Browser
Author:      Greg Dubinovskiy 
Email:       siccolo_mobile_management@yahoo.com
Environment: VB.NET
Keywords:    VB .NET WMI, Network Browser, Domain Information, Enumerate Computers on Network, 
		Enumerate Users on Network, Enumerate Services
Level:       Intermediate
Description: Build Simple Network Browser to Enumerate Computers on the Local Network
Section      Miscellaneous
SubSection   General

Siccolo Development Articles - Using WMI to build Network Browser, Using WMI to retrieve Domain Information, Enumerate Computers on Network with WMI, .NET Software Development - WMI connections made easy in .NET - Get System Information Using WMI in .NET - NET Framework's Windows Management Instrumentation (WMI) services
Google

Introduction

While working on SQL Server management tool for mobile devices - Siccolo, I created a simple applet to be able to browse computers on the local network within a domain and retrieve additional information (such as list of users, services).
See more at Articles from Siccolo!

The code presented allows to build simple network browser


See also:
How to check Shared Open Files using WMI
Build Windows Event Log Watcher Service Process to Export Event Log Entries as RSS feed using WMI - ManagementEventWatcher
Using System.ServiceProcess.ServiceController in .NET - Manage Services

Using the code

.NET allows to retrieve almost any information about local network by using WMI - Windows Management Instrumentation - service with ManagementObjectSearcher class and in conjunction with Active Directory Service Interfaces (ADSI) - Active Directory hierarchy.



The code is broken into several methods:
  1. Get Domain Name location computer belongs to:

    	// VB.NET //
    	...
       Public Function GetLocalComputerInfo() As Boolean
    
            Dim query As ManagementObjectSearcher
            Dim queryCollection As ManagementObjectCollection
    
            Dim query_command As String = "SELECT * FROM Win32_ComputerSystem"
    
            Dim msc As ManagementScope = New ManagementScope("root\cimv2")
    
            Dim select_query As SelectQuery = New SelectQuery(query_command)
    
            query = New ManagementObjectSearcher(msc, select_query)
            queryCollection = query.Get()
    
            Dim management_object As ManagementObject
    
            For Each management_object In queryCollection
                m_local_domain_name = management_object("Domain")
                m_local_computer_name = management_object("Name")
            Next management_object
    
            Return True
        End Function
    	...
    
    where:
    msc As ManagementScope sets a scope for management operations - defines the WMI namespace in which management operations are performed.

    Once domain name is known, we are able to retrieve list of computers within that domain.



  2. Enumerate computers within a domain:

    	// VB.NET //
    	...
        Public Function GetComputersInfoCollection(ByVal domain As String) As DirectoryEntry
            Dim domainEntry As DirectoryEntry = New DirectoryEntry("WinNT://" + domain)
            domainEntry.Children.SchemaFilter.Add("computer")
            Return domainEntry
        End Function
    	...
    
    where:
    method returns instance of DirectoryEntry - node in in the Active Directory hierarchy. that corresponds to the given domain.

    Next, let's retrieve list of users that belong to that domain.

  3. Enumerate users within a domain:

    	// VB.NET //
    	...
        Public Function GetUsersInfoCollection(ByVal domain As String) As ManagementObjectCollection
            Dim query As ManagementObjectSearcher
            Dim queryCollection As ManagementObjectCollection
    
            Dim msc As ManagementScope = New ManagementScope("root\cimv2")
            Dim query_command As String = _
    		"SELECT * FROM Win32_UserAccount  WHERE Domain=" & _
    			Chr(34).ToString() & domain & Chr(34).ToString()
            'Win32_UserAccount:
            'see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_useraccount.asp
            'class Win32_UserAccount : Win32_Account
            '{
            '  uint32 AccountType;
            '  string Caption;
            '  string Description;
            '  boolean Disabled;
            '  string Domain;
            '  string FullName;
            '  datetime InstallDate;
            '  boolean LocalAccount;
            '  boolean Lockout;
            '  string Name;
            '  boolean PasswordChangeable;
            '  boolean PasswordExpires;
            '  boolean PasswordRequired;
            '  string SID;
            '  uint8 SIDType;
            '  string Status;
            '};
            '
            Dim select_query As SelectQuery = New SelectQuery(query_command)
    
            query = New ManagementObjectSearcher(msc, select_query)
            queryCollection = query.Get()
    
            Return queryCollection
        End Function
    	...
    


    In addition to be able to retrieve above information, we can also obtain services collection for a given machine:

  4. Enumerate services on a computer using WMI:


    	// VB.NET //
    	...
        Public Function GetServicesInfoCollection(ByVal computer_name As String) As ManagementObjectCollection
            Dim query As ManagementObjectSearcher
            Dim queryCollection As ManagementObjectCollection
    
            Dim msc As ManagementScope = New ManagementScope("\\" & computer_name & "\root\cimv2")
            Dim query_command As String = "SELECT * FROM Win32_Service"
            'Win32_UserAccount:
            'see http://msdn.microsoft.com/library/en-us/wmisdk/wmi/win32_service.asp?frame=true
            '        class Win32_Service : Win32_BaseService
            '{
            '  boolean AcceptPause;
            '  boolean AcceptStop;
            '  string Caption;
            '  uint32 CheckPoint;
            '  string CreationClassName;
            '  string Description;
            '  boolean DesktopInteract;
            '  string DisplayName;
            '  string ErrorControl;
            '  uint32 ExitCode;
            '  datetime InstallDate;
            '  string Name;
            '  string PathName;
            '  uint32 ProcessId;
            '  uint32 ServiceSpecificExitCode;
            '  string ServiceType;
            '  boolean Started;
            '  string StartMode;
            '  string StartName;
            '  string State;
            '  string Status;
            '  string SystemCreationClassName;
            '  string SystemName;
            '  uint32 TagId;
            '  uint32 WaitHint;
            '};
    
            Dim select_query As SelectQuery = New SelectQuery(query_command)
    
            query = New ManagementObjectSearcher(msc, select_query)
            queryCollection = query.Get()
    
            Return queryCollection
        End Function  
    	...
    

    Points of Interest

    If you would like to read more - please take a look at Siccolo - Free Mobile Management Tool For SQL Server and more articles at Development Articles from Siccolo!

    History

    no improvements so far. nearly perfect.

    WMI connections made easy in .NET - Get System Information Using WMI in .NET - NET Framework's Windows Management Instrumentation (WMI) services allows ...


    Article keywords: WMI, Windows Management Instrumentation, ManagementObjectSearcher, Active Directory hierarchy, ManagementObjectCollection, Win32_ComputerSystem, ManagementScope, SelectQuery, SelectQuery.Get(), ManagementObject, DirectoryEntry, DirectoryEntry("WinNT://"), Win32_UserAccount, Win32_Service, System.ServiceProcess.ServiceController, .Net enumerate computers users active directory on network

    Back To Articles Page

    Free Mobile Management For SQL Server(s!) - Siccolo - SQL Management ToolQuestions? Suggestions? Concerns? - email me to siccolo_mobile_management@yahoo.com    Greg Dubinovsky 2006
    or share your thoughts at Siccolo Blog

    Web being sponsor - Mid-Atlantic Processing. Well being sponsor - Clarity MediSpa. Hairless sponsor - Clarity MediSpa Laser Hair Removal.
    Siccolo - SQL Server Management Tool For Mobile Devices is packed with built-in functionality and tools. Siccolo delivers a rich set of management tools for both DBAs and sys admins. SQL Server management has always been an area of DBA concern. The new Management Tool For Mobile Devices - Siccolo - has simple "Enterprise Manager" and the "Query Analyzer". Siccolo is a management tool for the MS SQL Server with administration capabilities and a database query tool. The administration features provide users the ability to browse database structures. An integrated query tool allows users to quickly create, edit and execute SQL queries and scripts. Siccolo also provides an export tool to allow users to easily save and email execution results. Siccolo helps database professionals save time and increase their productivity by utilizing a more efficient approach to database management - use their Windows Mobile empowered device while sipping margarita on the beach For increased security, Siccolo is configured to run under SSL with IIS authentication.
      Siccolo features are:
    • Run SQL Query window (with semi-automated Insert/Update/Delete/Select Statements) Open/Save SQL Scripts in multiple tabs Save/Email results
    • Explore SQL Server Browse databases and database objects (tables/views/stored procedures) Browse/Manage SQL Server jobs Browse/Manage windows services (start/stop) View event logs (Application, System)
    • Manage SQL Server - restart MS SQL Server service and SQL Agent