Solved

Show users logged into (w2k) domain/server

Posted on 2006-10-25
16
412 Views
Last Modified: 2012-06-27
Hi,

Does anyone know how to enumerate a list of the users logged into either the domain, or a specific domain controller (either is fine)?  
For the application I'm developing I'd need to be able to do this for normal (none-administrator) users using XP Pro workstations (Framework v2) within the same domain.

I've looked on MSDN and googled aroudn and only seem to find ways of showing users on local machines.  Anyone been here before and had success?
Thanks,

- Moz
0
Comment
Question by:nikez2k4
  • 5
  • 5
  • 3
16 Comments
 
LVL 15

Accepted Solution

by:
ozymandias earned 500 total points
ID: 17802207
OK. This will only be partially helpful.

There is a sysinternals utility called PsLoggedOn which will tell you who is logged on to a specific machine/server.
It comes with source code so you could get a look at how it's done and see if you can either reuse it or replicate its behaviour.

http://www.sysinternals.com/Utilities/PsLoggedOn.html

Basically it uses the NetEnumSession API.
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 17802379
Thanks for the link - Yeah, that's in C as well (you think my c# is bad, you should see my C!) :-)
I had a google around for NetEnumSession API, could only find a few 404s and nothing much of any help.  Thanks for taking the time tho.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17802503
You can also do this using WMI in C#.

Basically you can query all the sessions on a local machine like this :

using System;
using System.Management;

class WMITest{

    public static int Main(string[] args) {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_SessionConnection");
        foreach (ManagementObject session in searcher.Get()) {
      foreach (PropertyData p in session.Properties){
            Console.WriteLine(p.Name + " = " + p.Value);
            Console.ReadLine();
      }
        }
        return 0;
    }
}

You can also connect to the WMI interface on a remote machine and do the same thing.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
LVL 1

Author Comment

by:nikez2k4
ID: 17802667
Thanks for the reply, yeah - I'd been looking at WMI stuff on the MSDN but couldnt see how to make it search all the workstations out there (or simply query the FR DC) - I've tried your code example - compiles fine but only returns the username of the PC I'm on now (me!), rather than everyone.  Is this what you meant?
Thanks again.
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17803572
The example shows querying the local WMI provider.
You can coonect to another machine and run a similar query against that to find out who is logged on to to it both locally and remotely.
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 17803746
That'd be okay for a few machines but I'd (ideally) want to get all the users logged in and there are over 700 PCs/Laptops on the network so it'd be too many to add in for looking "manually" .
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17803905
It should not be too hard.

You can :

    get a lits of all machine accounts from the domain
    go thourgh each one and poll the machine to see if it's active/switched on
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17803991
ooops...

    if its active
       remotely query its WMI to see who's logged on
    if not
       skip

That should do it.

In your question you said you wnated to see users logged on to a domain or specific domain controller in which case you wouldn't have to query each PC just the servers in question.
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17805073
What operating system is your domain controller running?
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 17809897
Windows 2000 (Will be upgraded to 2003 in around 5 months)
All workstations are using XP Pro.
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17813313
The WMI suggestion is ideal, because it would only have to be run against the server in question (not on all the workstations).  To query WMI on a remote machine, you can modify ozymandias' code:

//If authentication is required.

  System.Management.ConnectionOptions connection = new System.Management.ConnectionOptions();
  connection.Username = "administrator";
  connection.Password = "password";

  System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\SERVER_IP_ADDRESS\root\cimv2", connection);

  ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(scope, "SELECT * FROM Win32_SessionConnection");

//If authentication is not required:

  System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(@"\\SERVER_IP_ADDRESS\root\cimv2", "SELECT * FROM Win32_SessionConnection");

The catch is, it requires Windows Server 2003.
0
 
LVL 1

Author Comment

by:nikez2k4
ID: 17829283
Okay, I'll hang-fire until the upgrade, Thanks for your help!
0
 
LVL 11

Expert Comment

by:Expert1701
ID: 17997463
nikez2k4, I would like to suggest you accept ozymandias's first comment as the solution as it was the only one offered that works on your current operating system.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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