Enumerate User Accounts of a Remote Machine

Posted on 2009-05-24
Medium Priority
Last Modified: 2013-11-10

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 http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21760016.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
            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")
            query = New ObjectQuery("SELECT * FROM Win32_Account")
            searcher = New ManagementObjectSearcher(scope, query)
            queryCollection = searcher.Get()
            For Each m As ManagementObject In queryCollection
                strAccount = (m.Item("Name"))
            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
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

Question by:msyed1
  • 2
  • 2
LVL 13

Accepted Solution

kaylanreilor earned 2000 total points
ID: 24464561
Probably you're enumerating all the user account that the computer can know from the whole domain !
If you have a look to the following discussion (http://www.pcreview.co.uk/forums/thread-531890.php), you'll see that one of the propositions made if to add a WHERE close to the WMI query:
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_UserAccount Where Domain = '" & strComputer & "'")
So, let's try this.
LVL 13

Expert Comment

ID: 24569384
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 !

Author Comment

ID: 24593131
Sorry, I haven't been able to test this out yet....but I willl soon. msyed1.    

Author Closing Comment

ID: 31584824
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.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question