wveagle81 has asked for the wisdom of the Perl Monks concerning the following question:

Using the Win32::OLE module, how would one get the currently logged in Windows user?
I found some information online, but it's mostly for VB users. I can use code I found
(looking at the Win32_LogonSession where LogonType=2) to get some information, but it's not
exactly what I want. I need the actual user name (not a number) and when their session
started.

Any help would be appreciated. I'm running this against Windows 7 workstations.

  • Comment on Query current logged in Windows user via WMI

Replies are listed 'Best First'.
Re: Query current logged in Windows user via WMI
by VinsWorldcom (Prior) on Jun 04, 2013 at 19:45 UTC

    You mentioned Win32::OLE but also mention "WMI" in your title. If you're using an external program, how about 'query':

    VinsWorldcom@C:\Users\VinsWorldcom> which query C:\Windows\system32\query.exe VinsWorldcom@C:\Users\VinsWorldcom> query /? QUERY { PROCESS | SESSION | TERMSERVER | USER } VinsWorldcom@C:\Users\VinsWorldcom> query user /? Display information about users logged on to the system. QUERY USER [username | sessionname | sessionid] [/SERVER:servername] username Identifies the username. sessionname Identifies the session named sessionname. sessionid Identifies the session with ID sessionid. /SERVER:servername The server to be queried (default is current). VinsWorldcom@C:\Users\VinsWorldcom> query user USERNAME SESSIONNAME ID STATE IDLE TIME LOGON + TIME >VinsWorldcom console 1 Active none 5/21/ +2013 7:03 PM

    Not sure if the 'query' command functionality is in Win32::OLE.

    Also note that the command 'net user USERNAME' returns the last logon time, but it is reset even if the user locks the screen and then unlocks it.

      Notice the last option:

      VinsWorldcom@C:\Users\VinsWorldcom> query user /? Display information about users logged on to the system. QUERY USER [username | sessionname | sessionid] [/SERVER:servername] username Identifies the username. sessionname Identifies the session named sessionname. sessionid Identifies the session with ID sessionid. /SERVER:servername The server to be queried (default is current).

      I guess I should have been more clear in my description. I'm writing a script to gather
      information about remote workstations. I'm using the Win32::OLE module to do queries
      against those remote workstations. One of the things I want to get is the name and session
      start date/time of the currently-logged in user.

Re: Query current logged in Windows user via WMI
by wveagle81 (Novice) on Jun 04, 2013 at 19:11 UTC

    Update: I can get the currently logged on user by looking at the UserName property in
    Win32_ComputerSystem, but it doesn't tell me when they logged on.

Re: Query current logged in Windows user via WMI
by Anonymous Monk on Jun 04, 2013 at 22:31 UTC