Link to home
Start Free TrialLog in
Avatar of msyed1
msyed1

asked on

Enumerate User Accounts of a Remote Machine

Hello,

I have written a console program that takes in a list of computer names. I need it to go through a loop using each computername and bring back an arraylist of all the Users that are defined on that remote machine.  I did a search through experts exchange and found this https://www.experts-exchange.com/questions/21760016/WMI-in-vb-2005-tutorials-or-examples.html
I am using the solution given in this PAQs, but for me what is happening is that it takes a very LOOONG time to run and it may be getting user accounts from more than one machine.  Please see the image attached from a debug session.  When it crashed it had 5220 as the length of the arraylist.  Ther is no way that one computername has that many accounts defined on it.  Could it getting the accounts from the entire domain ? I am attaching the code snippet I am using as well as the Log4net log.   Hope some one can help me with this.  active_photo was the expert who answered the question I am referred to above, but anyone else, please help.      

All I need it to do is to bring back the Users defined on 1 machine that I am sending in through the parameter.  I then have to go through the arraylist and figure out if a particular user account is within it and then do other stuff.  PLEASE HELP.  msyed1.  
Public Function EnumerateLocalAccounts(ByVal machineName As String, ByRef bolIsAccessDenied As Boolean) As ArrayList
        ''''''bolIsAccessDenied is ByRef
 
        Dim arlArrayOfAccounts As New ArrayList
        Dim strAccount As String
 
        Dim strNumbers As String = "0,1,2,3,4,5,6,7,8,9"
        Dim strSplitNumbers() As String = Split(strNumbers, ",")
        Dim i As Integer = 0
 
        'If this is an IP Address....bypass
        For i = 0 To strSplitNumbers.GetUpperBound(0)
            If machineName.StartsWith(strSplitNumbers(i)) Then
                Return arlArrayOfAccounts
            End If
        Next
   
        Try
 
            Dim query As ObjectQuery
            Dim searcher As ManagementObjectSearcher
            Dim scope As ManagementScope = New ManagementScope
            Dim strComputerName As String = machineName
            Dim queryCollection As ManagementObjectCollection
            'Dim options As ConnectionOptions = New ConnectionOptions
            'options.Username = "testAccount"
            'options.Password = "fooo"
            'scope = New ManagementScope("\\" & strComputerName & "\root\cimv2", options)
 
            scope = New ManagementScope("\\" & strComputerName & "\root\cimv2")
 
            scope.Connect()
            query = New ObjectQuery("SELECT * FROM Win32_Account")
            searcher = New ManagementObjectSearcher(scope, query)
            queryCollection = searcher.Get()
            For Each m As ManagementObject In queryCollection
                'ListBox1.Items.Add(m("caption"))
                strAccount = (m.Item("Name"))
                arlArrayOfAccounts.Add(strAccount)
            Next
 
            Return arlArrayOfAccounts
 
        Catch uae As UnauthorizedAccessException
            log.Error("Error occurred on machinename: " & machineName & " - " & uae.Message)
            bolIsAccessDenied = True
 
        Catch ex As Exception
            log.Error("Error occurred: " & ex.Message)
        End Try
 
    End Function
 
 
Log4Net:
2009-05-24 10:31:47,969 [4996] ERROR [app(TaskRunnerAccountDiscovery.exe) class(TaskRunnerAccountDiscovery) method(EnumerateLocalAccounts) line(203)] - Error occurred: The remote procedure call failed.
2009-05-24 10:34:39,508 [4996] ERROR [app(TaskRunnerAccountDiscovery.exe) class(TaskRunnerAccountDiscovery) method(Main) line(119)] - Error: Object reference not set to an instance of an object.

Open in new window

DebugSession.doc
ASKER CERTIFIED SOLUTION
Avatar of kaylanreilor
kaylanreilor
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is it possible that you give some news after the thread I posted ? If other people face the same issue they would probably need to know what do you think about my answer !
Avatar of msyed1
msyed1

ASKER

Sorry, I haven't been able to test this out yet....but I willl soon. msyed1.    
Avatar of msyed1

ASKER

kaylanreilor:  I still haven't had the opportunity to test your suggestion.  But I wanted to go ahead and award you the points.  Thank you for your assistance and I am sorry for the delay.  msyed1.