Getting All Logged On Users and IP VB.net
-
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
-
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
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 -
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 KreskowiakThanks 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.
-
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.
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 -
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 KreskowiakThanks 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.
-
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.
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 -
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 KreskowiakThanks 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!!
-
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!!
Nope. I've never had to do what you're doing.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Nope. I've never had to do what you're doing.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Great. Glad you got something that works for you.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak