Advertisement
jcunews

GetLoggedInUserName.vbs

Jan 18th, 2024 (edited)
1,607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'GetLoggedInUserName v1.0.1, January 18th 2024.
  2. 'https://www.reddit.com/user/jcunews1
  3. 'https://pastebin.com/u/jcunews
  4. 'https://greasyfork.org/en/users/85671-jcunews
  5. '
  6. 'Display the user name of the oldest user account which was logged into
  7. 'interactive session. This script is intended to be run by a different
  8. 'user account in any logon session.
  9. '
  10. 'If there no user account logged into the interactive session,
  11. 'nothing will be displayed, and the script ends with exit code 1.
  12.  
  13. set wm = getobject("winmgmts:")
  14.  
  15. 'get oldest interactive logon session ID
  16. set rs = wm.execquery("select * from win32_logonsession")
  17. lsid = 0
  18. lsst = 1e300
  19. for each ls in rs
  20.   if ls.logontype = 2 then 'interactive session
  21.    st = cdbl(left(ls.starttime, 21))
  22.     if st < lsst then
  23.       lsid = ls.logonid
  24.       lsst = st
  25.     end if
  26.   end if
  27. next
  28.  
  29. 'get first user name logged into oldest interactive session
  30. set rs = wm.execquery("select * from win32_loggedonuser")
  31. username = ""
  32. for each lu in rs
  33.   if instr(lu.dependent, """" & lsid & """") > 0 then
  34.     username = mid(lu.antecedent, instr(lu.antecedent, "Name=") + 6)
  35.     username = left(username, len(username) - 1)
  36.     exit for
  37.   end if
  38. next
  39.  
  40. if username <> "" then
  41.   wsh.echo username
  42. else
  43.   wsh.quit 1
  44. end if
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement