Anyone else had to determine who the currently logged on user is remotely in a Windows 7 environment? I am using .NET 4.0 and C#. The environment is mixed XP and 7.
- WMI queries involving sessions result in all active sessions, but not the session that is interactive.
- UserName from ComputerSystem (WMI) returns null exception if user is connected via Remote Desktop, which is common enough that this method cannot be used.
PsLoggedOn
takes too long for my purposes (yes, 300 ms is too long) and is surprisingly not accurate 100% of the time- Using p/invoke for
WTSGetActiveConsoleSessionID
orLsaEnumerateLogonSessions
is too complicated and prone to memory leaks (from what I've read) tasklist /S <computername>
will return information for XP systems, but Windows 7 won't be agreeable thanks to that lovely UAC.- HKCU (win registry) is inaccessible remotely due to permissions restrictions, HKU is accessible, but Volatile Environment doesn't appear to have a tag for "active"
So far, the most reliable way is using PsExec
to remotely execute qwinsta
from the command line and traverse the output to text remotely. This is annoying and takes time (more than PsLoggedOn
), but I'm running out of ideas here for reliability. Reliability before speed, but speed is very important in terms of cost benefit.
Third party tools are not an option, has to be a script, preferably WMI and C#. I delved into hitting the DC using Principal objects, but I'm afraid I might have confused myself more. Also, all user accounts are administrators.
I've done a lot of research over Google, but I am thinking that maybe I am looking in the wrong place.
Any takers?