Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Username over network

Status
Not open for further replies.

Monkey36

MIS
May 23, 2005
60
GB
Hi all -

This is something I used to be able to do in VB6 using a set of API calls I can't remember anymore, but was hoping that there would be an easy way to do it in VB.Net.

I'm relatively new to .Net so you may have to go easy on me.

Using System.DirectoryServices I've been able to use ADSI to pick out a list of computers on the network, and then used My.Computer.Network.Ping to find out which are available, but what I want to do now is find out who is logged on to each of the on-line PCs.

Nowhere can I find out how to do this, or whether it's possible. I've been googling for ages and either the information is not very visible or my googling skills are a bit off today!

Thanks in advance to anyway that point me in the right direction.

Cheers,
Dan.
 
Once you've got the list of IP address, you could shell to NBTSTAT commandline utility to determine that information amd save the output to a file for later processing.


This is from NBTSTAT help:

Displays protocol statistics and current TCP/IP connections using NBT
(NetBIOS over TCP/IP).

NBTSTAT [ [-a RemoteName] [-A IP address] [-c] [-n]
[-r] [-R] [-RR] [-s] [-S] [interval] ]

-a (adapter status) Lists the remote machine's name table given its name
-A (Adapter status) Lists the remote machine's name table given its
IP address.
-c (cache) Lists NBT's cache of remote [machine] names and their IP
addresses
-n (names) Lists local NetBIOS names.
-r (resolved) Lists names resolved by broadcast and via WINS
-R (Reload) Purges and reloads the remote cache name table
-S (Sessions) Lists sessions table with the destination IP addresses
-s (sessions) Lists sessions table converting destination IP
addresses to computer NETBIOS names.
-RR (ReleaseRefresh) Sends Name Release packets to WINS and then, starts Refr
esh

RemoteName Remote host machine name.
IP address Dotted decimal representation of the IP address.
interval Redisplays selected statistics, pausing interval seconds
between each display. Press Ctrl+C to stop redisplaying
statistics.

see also: for more information

Hope this helps.
 
Hmm.. unfortunately that approach was not able to give me the username of a logged on user - I read the linked article which implied that it should, but when I tried it myself it didn't (it showed the computer name and domain name, but no user name - maybe because I'm on XP and the article was referring to NT??)

Also, I was really hoping that there would be a built-in way of obtaining user name, rather than having to read in the output of another program.

Thanks for your suggestions though!
And any more flowing this will way will be much appreciated!

Dan.
 
I use this code to return the full name of the person logged on to the local machine. I know you can alter it slightly to get the name of the person logged onto any specific machine. Although I can't remember how specificly, I would start by trying to add the target machine in the Directory Entry path:

Code:
    Public Shared Function UserNameLong(ByVal UserID As String)
      Dim FullName As String
      Try
        Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://Domain/" & UserID.Trim)
        FullName = ADEntry.Properties("FullName").Value
      Catch exc As Exception
        FullName = ""
      End Try
      Return FullName
    End Function

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick, thanks for the suggestion but I don't think that's the correct code for what I want to do - how it looks to me is that it queries AD to retrieve the full name of the user whose UserID you pass to the function, but you need to know the UserID in the first place, which has been my problem.

I've nearly got it sorted now, I think. I've used some .Net WMI code in the end which seems to be doing the job. The only problem so far is that one of the computers is returning a blank UserName when I know someone is in fact logged onto it.

Anyway, here's the code I'm using in case anyone's got any suggestions or it comes in useful in the future.

Code:
  Private Function GetUserName(ByVal ComputerName As String) As String
    [COLOR=green]' Set up WMI connection options[/color]
    Dim objConnOptions As New ConnectionOptions
    With objConnOptions
      .Impersonation = ImpersonationLevel.Impersonate
      .Authentication = AuthenticationLevel.Default
    End With

    [COLOR=green]' Try to connect to specified computer[/color]
    Dim objScope As ManagementScope
    Try
      objScope = New ManagementScope(, _
      "\\" & ComputerName & "\root\cimv2", objConnOptions)
      objScope.Connect()
    Catch
      Return "Conn Error (1)"
    End Try
    If Not (objScope.IsConnected) Then
      Return "Conn Error (2)"
    End If

    [COLOR=green]' Try to access details through connection[/color]
    Dim objQuery As New Management.ObjectQuery
    objQuery.QueryString = "Select * From Win32_ComputerSystem"
    Dim objSearcher As ManagementObjectSearcher
    Dim objComputer As ManagementObjectCollection
    Try
      objSearcher = New ManagementObjectSearcher(objScope, objQuery)
      objComputer = objSearcher.Get
    Catch
      Return "Conn Error (3)"
    End Try

    [COLOR=green]' Ensure successfully retrieved data before treating
    ' objComputer as a collection[/color]
    If (objComputer Is Nothing) Then
      Return "Conn Error (4)"
    End If

    [COLOR=green]' Retrieve user name (objComputer is an object collection
    ' but we only expect it to have 1 item in it)[/color]
    For Each ComputerItem As ManagementObject In objComputer
      If (ComputerItem("UserName") <> "") Then
        Return ComputerItem("UserName")
      End If
    Next

    [COLOR=green]' Failed[/color]
    Return "Conn Error (5)"
  End Function
 
Can you give me some more information regarding this process? If I use

Code:
    Dim ComputerName As String = "CH24"
    Try
      Dim ADEntry As New System.DirectoryServices.DirectoryEntry( _
          "WinNT://Claire/" & ComputerName)
      MsgBox("Computer: " & ADEntry.Properties("Name").Value & vbCrLf & _
             "User: " & ADEntry.Properties("FullName").Value)
    Catch ex As Exception
      MsgBox(ex.Message)
    End Try

then it picks up the computer object, and reports the Name property to be "CH24", as I passed in, but I don't know what property I need to get the logged-in user. "FullName" returns blank.

Thanks again,
Dan.
 
My own knowledge of AD naming standards is pretty limited, but I would guess SN might be the value you are looking for. FullName is a element of the User object. I'll see what I can dig up.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
You might want to check out this article: it's got a pair of nice examples on AD and WMI. And the article here: shows how to get a list of computers, you may be able to loop through the collections on each entry to find the user name.

I know it's in there someplace, I ran into it completely on accident last year.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Did it hurt?

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Almost. I had accidentally run into a complete list of machines, installed software, and current logged on users. The networker guys said "Hey, that's cool, can you make that a standard report module and send it to us monthly?"

But I still have way to much work lined up to actually get to it.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks again for the pointers, but I'm still hitting a brick wall. From what I can see, you can retrieve a list of computers on the domain, and a list of users on the domain as well as a list of local user accounts for any specified computer. However, I don't think you can retrieve which user is logged on at which computer at any given point in time - I'm quite this information isn't actually logged in AD.

I'm also having problems with the code I posted above, where different versions of WMI seem to cause problems retrieving usernames.

Why is this so hard?? Seems like it should be easy? :(

Oh well, I'll keep looking.

Again, if anyone has any ideas of suggestions, you'd make me a very happy boy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top