Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Getting All Logged On Users and IP VB.net

Getting All Logged On Users and IP VB.net

Scheduled Pinned Locked Moved Visual Basic
csharpcss
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DinoRondelly
    wrote on last edited by
    #1

    I am trying to get all logged on users and their IPAddresses and display them in a data grid veiw in a windows form using VB.net I have tried the below code and it appears to get the users on a windows 7 machine but not on servers. Any information on this would be greatly appreciated.

    Public Shared Function GetUsers() As List(Of Users)

    Dim UserList As New List(Of Users)
    
    Try
        ' WhereDomain=""BLABLA"""
        Using searcher = New ManagementObjectSearcher("root\\CIMV2", "SELECT \* FROM Win32\_LogonSession WHERE LogOnType = 2")
    
            For Each queryObj As ManagementObject In searcher.Get()
    
                Dim qry As String = "Associators of " \_
                                 & "{Win32\_LogonSession.LogonId=" & queryObj.GetPropertyValue("LogonId") & "} " \_
                                 & "Where AssocClass=Win32\_LoggedOnUser Role=Dependent"
    
                Using iSearch = New ManagementObjectSearcher(qry)
    
                    For Each res As ManagementObject In iSearch.Get()
    
                        Dim ret As New Users(res.GetPropertyValue("Name"), \_
                                         "", "")
    
                        UserList.Add(ret)
                    Next
    
    
                End Using
            Next
        End Using
    
        Return UserList
    
    Catch err As ManagementException
    
        MessageBox.Show(err.Message)
        Return UserList
    
    End Try
    

    End Function

    D 1 Reply Last reply
    0
    • D DinoRondelly

      I am trying to get all logged on users and their IPAddresses and display them in a data grid veiw in a windows form using VB.net I have tried the below code and it appears to get the users on a windows 7 machine but not on servers. Any information on this would be greatly appreciated.

      Public Shared Function GetUsers() As List(Of Users)

      Dim UserList As New List(Of Users)
      
      Try
          ' WhereDomain=""BLABLA"""
          Using searcher = New ManagementObjectSearcher("root\\CIMV2", "SELECT \* FROM Win32\_LogonSession WHERE LogOnType = 2")
      
              For Each queryObj As ManagementObject In searcher.Get()
      
                  Dim qry As String = "Associators of " \_
                                   & "{Win32\_LogonSession.LogonId=" & queryObj.GetPropertyValue("LogonId") & "} " \_
                                   & "Where AssocClass=Win32\_LoggedOnUser Role=Dependent"
      
                  Using iSearch = New ManagementObjectSearcher(qry)
      
                      For Each res As ManagementObject In iSearch.Get()
      
                          Dim ret As New Users(res.GetPropertyValue("Name"), \_
                                           "", "")
      
                          UserList.Add(ret)
                      Next
      
      
                  End Using
              Next
          End Using
      
          Return UserList
      
      Catch err As ManagementException
      
          MessageBox.Show(err.Message)
          Return UserList
      
      End Try
      

      End Function

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Unless the users are logged in on Remote Desktop (or Terminal Services) to the server, this code won't show you anything on the server. LogOnType = 2 is Interactive logons only. Remove the WHERE clause and see what you get.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        Unless the users are logged in on Remote Desktop (or Terminal Services) to the server, this code won't show you anything on the server. LogOnType = 2 is Interactive logons only. Remove the WHERE clause and see what you get.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        D Offline
        D Offline
        DinoRondelly
        wrote on last edited by
        #3

        Thanks so much for the reply What I am trying to do is determine all users logged on to a server and then get their IPAdresses and any other information I can get on them. I did what you suggested but for some reason this also included Local Services,Network Services and others in the list of users. Also every user in the list was triplicated. This is the first project I have done in VB.net so forgive me if i come off as a novice. Any information or code samples would be greatly appreciated.

        D 1 Reply Last reply
        0
        • D DinoRondelly

          Thanks so much for the reply What I am trying to do is determine all users logged on to a server and then get their IPAdresses and any other information I can get on them. I did what you suggested but for some reason this also included Local Services,Network Services and others in the list of users. Also every user in the list was triplicated. This is the first project I have done in VB.net so forgive me if i come off as a novice. Any information or code samples would be greatly appreciated.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          and what were their LogOnTypes?? I have no idea what kind of user you're looking for! "Logged on users" can mean anything depending on HOW they are logged on. Are they remote desktop sessions? HTTP Sessions? Mapped drives?? Each of these is a different logon type and needs to be handled differently.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          D 1 Reply Last reply
          0
          • D Dave Kreskowiak

            and what were their LogOnTypes?? I have no idea what kind of user you're looking for! "Logged on users" can mean anything depending on HOW they are logged on. Are they remote desktop sessions? HTTP Sessions? Mapped drives?? Each of these is a different logon type and needs to be handled differently.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            D Offline
            D Offline
            DinoRondelly
            wrote on last edited by
            #5

            Thanks for the reply, I am trying to get all users that are on the server that have remoted in and their IPAddresses. Similar to what is showing in the task manager under the users tab and in the terminal service manager.

            D 1 Reply Last reply
            0
            • D DinoRondelly

              Thanks for the reply, I am trying to get all users that are on the server that have remoted in and their IPAddresses. Similar to what is showing in the task manager under the users tab and in the terminal service manager.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              That's obtained from the WMI provider for Remote Desktop. You're using the LogOnSession class, which is a bit too high level for what you're looking for. You can find the docs on these classes here[^].

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              D 1 Reply Last reply
              0
              • D Dave Kreskowiak

                That's obtained from the WMI provider for Remote Desktop. You're using the LogOnSession class, which is a bit too high level for what you're looking for. You can find the docs on these classes here[^].

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                D Offline
                D Offline
                DinoRondelly
                wrote on last edited by
                #7

                Thanks for the reply, I will look into what you have suggested, do you by any chance have any code samples of how this works? If not no worries, Thanks!!

                D 1 Reply Last reply
                0
                • D DinoRondelly

                  Thanks for the reply, I will look into what you have suggested, do you by any chance have any code samples of how this works? If not no worries, Thanks!!

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Nope. I've never had to do what you're doing.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  D 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Nope. I've never had to do what you're doing.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    D Offline
                    D Offline
                    DinoRondelly
                    wrote on last edited by
                    #9

                    http://www.codeproject.com/Articles/111430/Grabbing-Information-of-a-Terminal-Services-Sessio Works perfectly

                    D 1 Reply Last reply
                    0
                    • D DinoRondelly

                      http://www.codeproject.com/Articles/111430/Grabbing-Information-of-a-Terminal-Services-Sessio Works perfectly

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      Great. Glad you got something that works for you.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups