Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get username of currently logged in user on remote client

Posted on 2004-10-27
9
Medium Priority
?
2,730 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
Comment
Question by:HatchIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:The_Biochemist
ID: 12420465
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
ID: 12421554
MsgBox(Environ("UserName"))

~b
0
 
LVL 8

Expert Comment

by:wguerram
ID: 12421640
Try this one:

MsgBox(SystemInformation.UserName)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

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

Accepted Solution

by:
prakash_prk earned 700 total points
ID: 12421979
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
ID: 12429458
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
ID: 12430246
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
ID: 12455999
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
ID: 12460566
Thanks HatchIT
see this link for more on WMI object
http://www.codeproject.com/csharp/wmi.asp

Regards
Prakash
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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