Detect Logged in User whle running as System

Answered Detect Logged in User whle running as System

  • Tuesday, November 06, 2012 11:54 AM
     
     

    Hi Guys

    I run a vbs script which is running as SYSTEM but needs to detect who the logged in user is , I've used the folowing code in XP which works just fine:

    "set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

    set colSessions = objWMI.ExecQery ("Select * from Win32_LogonSessions where LogonType = 2")

    for each objSession in ColSessions

     set ColList = objWMI.EXecQuery("Associators of " & "{Win32_LogonSession.LogonId" & objSession.LogonId & "} " & "Where AssocClass=Win32_LoggedOnUer Role=Dependent" )

     For Each objItem in ColList

    strRunningUser = objItem.Name

    next

    Next"

    This works fine on XP but on Win7 I find it is returning in ColList not only the name of the user who is currently logged in but also the names of people who used to be logged in but have now logged off !! Any suggestions other than only running the script after a restart.

All Replies

  • Tuesday, November 06, 2012 12:48 PM
     
     

    Under Windows 7 you could use this console command:

    query.exe user

    You might get your WMI code to work if you replace objWMI.ExecQery with objWMI.ExecQuery and Win32_LoggedOnUer with Win32_LoggedOnUser
  • Sunday, November 11, 2012 10:34 PM
     
     

    Appreciate the code was typed in wrong , but that's only because I can't cut and paste into this editor. The code works perfectly well in reality as my post originally said, at least so far as the absence of syntax errors. What I could really do with is getting an answer to the actual question, and as an aside another answer as to how the OS is even aware of the names of users who aren't even logged on any more let alone confused into thinking they still are.

  • Monday, November 12, 2012 1:14 AM
     
     

    Appreciate the code was typed in wrong , but that's only because I can't cut and paste into this editor. The code works perfectly well in reality as my post originally said, at least so far as the absence of syntax errors. What I could really do with is getting an answer to the actual question, and as an aside another answer as to how the OS is even aware of the names of users who aren't even logged on any more let alone confused into thinking they still are.

    You need the 'Username'property of win32_computersystem.  This always shows the current console user if logged on.

    This is the only way you can do this with WMI from the SYSTEM account on Windows 7.


    ¯\_(ツ)_/¯


  • Monday, November 12, 2012 6:12 AM
     
     Answered

    Appreciate the code was typed in wrong , but that's only because I can't cut and paste into this editor.

    Seems a little unfair of you shifting the burden of checking the spelling of your code to respondents. Anyway, building on JRV's suggestion, here is some code that you can run under the System account. It will report the name of the foreground session.

    Set oWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
    Set cItems = oWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48) 
    For Each oItem in cItems 
        Wscript.Echo "UserName=" & oItem.UserName
    Next