Solved

Get username of currently logged in user on remote client

Posted on 2004-10-27
2,713 Views
Last Modified: 2008-01-09
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
Question by:HatchIT
    9 Comments
     
    LVL 6

    Expert Comment

    by:The_Biochemist
    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
     
    LVL 8

    Expert Comment

    by:bramsquad
    MsgBox(Environ("UserName"))

    ~b
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    Try this one:

    MsgBox(SystemInformation.UserName)
    0
     
    LVL 8

    Expert Comment

    by:wguerram
    SystemInformation class has other interesting properties. you should take a look a those.
    0
     
    LVL 10

    Accepted Solution

    by:
    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
     
    LVL 1

    Author Comment

    by:HatchIT
    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
     
    LVL 10

    Expert Comment

    by:prakash_prk
    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
     
    LVL 1

    Author Comment

    by:HatchIT
    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
     
    LVL 10

    Expert Comment

    by:prakash_prk
    Thanks HatchIT
    see this link for more on WMI object
    http://www.codeproject.com/csharp/wmi.asp

    Regards
    Prakash
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    913 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now