Results 1 to 5 of 5

Thread: [RESOLVED] Getting UserName from Computer Name

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    17

    Resolved [RESOLVED] Getting UserName from Computer Name

    Hello to all.
    I was wondering if you can get the username of the person logged into a computer if I only have the computer name.
    Meaning I want to have some VB code to pass the computer name to and get the person presently logged in.

    Can this be done?

    Dane

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Getting UserName from Computer Name

    Try msome thing like this:
    Code:
    Private Sub Command1_Click()
    Dim strComputer As String
    Dim oWMIService As Object
    Dim oComputers As Object
    Dim oComputer As Object
    
    On Error Resume Next
        
        strComputer = "abc..."
        
        Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\xxxx\xxxxx")
        Set oComputers = oWMIService.ExecQuery("Select * from Win32_ComputerSystem")
        
        For Each oComputer In oComputers
            List1.AddItem oComputer.UserName
        Next
    
    End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    17

    Re: Getting UserName from Computer Name

    Thanks Rhino this seems to get me going but I still have two questions..
    Code:
    Public Function getUsrFromCompName(strComputer As String) As String
    
    Dim oWMIService As Object
    Dim oComputers As Object
    Dim oComputer As Object
    Dim strComp As Variant
    
    On Error Resume Next
          
        Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\xxxx\xxxxx")
        Set oComputers = oWMIService.ExecQuery("Select UserName from Win32_ComputerSystem")
        
        For Each oComputer In oComputers
           'Debug.Print oComputer.UserName
           getUsrFromCompName = oComputer.UserName
        Next
         
    End Function
    Question:

    1. The function call only seems to work when called from inside the loop. Why is that?
    2. It seems like it only works on my computer when I am logged on. I belive it is because of the permisions the company has setup. They are not allowing me to see the username on any other computer. Is is assumption correct? And if so what permissions i need to tell the system adminsitrator to give me, if you know.

    Again thanks for your help thus far.
    Dallr
    Last edited by Hack; Dec 23rd, 2009 at 08:26 AM.

  4. #4

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2006
    Posts
    17

    Re: Getting UserName from Computer Name

    Thanks I did not know that!

    Cheers!

Posting Permissions

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



Click Here to Expand Forum to Full Width