[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Get username of currently logged in user on remote client

Posted on 2004-10-27
9
Medium Priority
?
2,733 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Get the source code for a fully functional Access application shell with several popular security features that Access VBA application developers desire, but find difficult or impossible to figure out how to code. You get the source code for managi…
Suggested Courses

591 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