• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2745
  • Last Modified:

Get username of currently logged in user on remote client

Anyone know how to do this?  Basically, there are times when I have a computer name that I need to do something to, but I don't know who is using that computer.  Normally, I'd use 'nbtstat -a <computer>' and go by the <03> (Messenger) name registered with WINS, or failing that, connect to the C$ share and see what user profile is most recent.  I'd like to write a program that does this, as well as displays the IP address(es) for that host (which I've already coded).

Any help would be appreciated.  Code already in place is:

Imports System.Net

  Sub GetInfo()
    lstIP.Items.Clear()
    lstIP.Items.AddRange(GetIP(txtClient.Text))
    lstWINS.Items.AddRange(GetWINS(txtClient.Text))
  End Sub

  Function GetIP(ByVal strClient As String) As String()
    Dim hstClient As IPHostEntry = Dns.Resolve(strClient)
    Dim ipClient As IPAddress() = hstClient.AddressList
    Dim ipAddy As IPAddress
    Dim strAddress() As String
    Dim x As Integer = -1

    For Each ipAddy In ipClient
      x += 1
      ReDim Preserve strAddress(x)
      strAddress(x) = ipAddy.ToString
    Next

    Return strAddress

  End Function

  Function GetWINS(ByVal strClient As String) As String()
    Dim strAddress As String
    Dim strAddresses As String()
    Dim x As Integer = -1

    ReDim strAddresses(lstIP.Items.Count - 1)

    For Each strAddress In lstIP.Items
      x += 1
      strAddresses(x) = strAddress
    Next

    strAddress = ""

    For Each strAddress In strAddresses
      'NEED CODE HERE
    Next
  End Function

  Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
    End
  End Sub

  Private Sub cmdGetInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGetInfo.Click
    GetInfo()
  End Sub
End Class

cmdExit is self-explanatory.
cmdGetInfo is the command button to press to, well, Get the Info.  :-P
lstIP is a list box to hold all IP addresses from GetIP().
lstWINS is a list box to hold all WINS information returned.
txtClient is a text box for entering the PC name you want info from.

PS: I'm teaching myself VB.NET (as you might have guessed from the code above), and would welcome any pointers on how I could do things better.
0
HatchIT
Asked:
HatchIT
  • 3
  • 2
  • 2
  • +2
1 Solution
 
The_BiochemistCommented:
Hi,

Assuming the thread is not started by code that is impersonating a user then

dim strUserName as string = environment.username

Should do it

If you want to know the domain and username then

dim strDomainUserNane as string = environment.username & "@" & environment.UserDomainName


0
 
bramsquadCommented:
MsgBox(Environ("UserName"))

~b
0
 
wguerramCommented:
Try this one:

MsgBox(SystemInformation.UserName)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
wguerramCommented:
SystemInformation class has other interesting properties. you should take a look a those.
0
 
prakash_prkCommented:
Try this
--------------------------
Dim i As New System.Management.ConnectionOptions
            i.Username = "your user name"
            i.Password = "your password "
            i.Authentication = AuthenticationLevel.Connect
            i.EnablePrivileges = True
            i.Impersonation = ImpersonationLevel.Impersonate


            Dim MScope As New ManagementScope("\\ngws09xp\root\cimv2")
            Dim OQ As New ObjectQuery("SELECT * FROM WIN32_ComputerSystem")
            Dim MS As New ManagementObjectSearcher(MScope, OQ)
            Dim MC As ManagementObjectCollection = MS.Get
            Dim MO As ManagementObject

            For Each MO In MC
                MsgBox(MO("UserName"))
                MsgBox(MO("Domain"))
                Exit For
            Next
------------------------
check this links.
http://groups.google.com/groups?hl=en&lr=&threadm=041501c2cebe%24ff277800%243301280a%40phx.gbl&rnum=27&prev=/groups%3Fq%3Dlogged%2Buser%252Bremote%2Bmachine%2B%252B%2Bvb.net%26start%3D20%26hl%3Den%26lr%3D%26selm%3D041501c2cebe%2524ff277800%25243301280a%2540phx.gbl%26rnum%3D27

Regards
Prakash
0
 
HatchITAuthor Commented:
Sorry guys, maybe I didn't make it clear enough that I'm looking for this info on a remote system, not the local PC.

Thanks for that Prakash, it worked.  Here's the new GetWINS function:

  Function GetWINS(ByVal strClient As String) As String()
    Dim strAddress As String
    Dim strAddresses As String()
    Dim strUserName As String()
    Dim x As Integer = -1

    ReDim strAddresses(lstIP.Items.Count - 1)

    For Each strAddress In lstIP.Items
      x += 1
      strAddresses(x) = strAddress
    Next

    strAddress = ""
    x = -1

    For Each strAddress In strAddresses
      Dim MScope As New ManagementScope("\\" & strAddress & "\root\cimv2")
      Dim OQ As New ObjectQuery("SELECT * FROM WIN32_ComputerSystem")
      Dim MS As New ManagementObjectSearcher(MScope, OQ)
      Dim MC As ManagementObjectCollection = MS.Get
      Dim MO As ManagementObject

      For Each MO In MC
        x += 1
        ReDim strUserName(x)
        strUserName(x) = CStr(MO("UserName"))
      Next

    Next
    Stop
    Return strUserName

  End Function


I didn't seem to need the "i" section, I'm not sure what that does.

However, whenever I run this and input a computer that's turned on but no-one is logged into it, it throws an error.  How would I use your method to catch that fact?
0
 
prakash_prkCommented:
Try this
you can pass MyConnectionOptions as second argument to the ManagementScope constructor
            Dim MScope As New ManagementScope("\\ngws09x2p\root\cimv2",MyConnectionOptions)
-----------------------------------------------------------------------
Try
            Dim MyConnectionOptions As New System.Management.ConnectionOptions
            MyConnectionOptions.Username = "your user name"
            MyConnectionOptions.Password = "your password "
            MyConnectionOptions.Authentication = AuthenticationLevel.Connect
            MyConnectionOptions.EnablePrivileges = True
            MyConnectionOptions.Impersonation = ImpersonationLevel.Impersonate


            Dim MScope As New ManagementScope("\\ngws09x2p\root\cimv2")
            Dim OQ As New ObjectQuery("SELECT * FROM WIN32_ComputerSystem")
            Dim MS As New ManagementObjectSearcher(MScope, OQ)
            Dim MC As ManagementObjectCollection

            Try
                MC = MS.Get

            Catch ex As System.Runtime.InteropServices.COMException
                If ex.Message = "The RPC server is unavailable." And ex.Source = "System.Management" Then
                    MsgBox("The machine may be shutdown or RPC Server is not running on that machine")
                    Exit Sub
                End If
            Catch ex As Exception
                MsgBox("Other" & ex.ToString)
            End Try


            Dim MO As ManagementObject

            For Each MO In MC
                MsgBox(MO("UserName"))
                MsgBox(MO("Domain"))
                Exit For
            Next
        Catch ex As Exception
                MsgBox(ex.ToString)
        End Try
-------------------------------------------------
Regards
Prakash
0
 
HatchITAuthor Commented:
Ah, so the ConnectionOptions object was just to specify a username and password if needed to get access to the machine to find out the logged in user, yes?

And I forgot about the Try function  :-P  That should be easy enough to implement.

Increased points for the extra answer.  Thanks for everything, Prakash.  :-)
0
 
prakash_prkCommented:
Thanks HatchIT
see this link for more on WMI object
http://www.codeproject.com/csharp/wmi.asp

Regards
Prakash
0
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

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now