Current User And Last Logged on Login Details

Hi,

This script provide you the details about the “Current Logged in User” details and also the “Last Logged on user details”.

Note :
1.Please run the script using the option “Run as Administrator” Via ITSM.
2.The Timestamp displayed in the DateTime Format (YYYYMMDDHHMMSS).



import os
import ctypes
import subprocess
import re
class disable_file_system_redirection:
    _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
    _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
    def __enter__(self):
        self.old_value = ctypes.c_long()
        self.success = self._disable(ctypes.byref(self.old_value))
    def __exit__(self, type, value, traceback):
        if self.success:
            self._revert(self.old_value)
with disable_file_system_redirection():
    current_user = os.popen('query user').read()
    user_details = os.popen("WMIC PATH Win32_NetworkLoginProfile GET   LastLogon,Name").read()
    if current_user == "":
        print "Current User : No user has logged in"
        print "                                               "
        print "The Last logged on details"
        for i in [i.strip() for i in user_details.split('
') if 'NT AUTHORITY' not in i if i.strip()]:
            print i
    else:
        print "The Current user details"
        print current_user
        print "                                               "
        print "The Last logged on details"
        for i in [i.strip() for i in user_details.split('
') if 'NT AUTHORITY' not in i if i.strip()]:
            print i



Sample Output 01:

[I]Example : When no user has logged into the system

[/I]

Sample Output 02:

[I]Example : When a user has logged into the system.

[/I]

20170428-Current_And_Last-Logged-On_User_Login_Details.json (2.11 KB)

This will be very handy for querying terminal servers, thank you. Still hope it’ll be considered to replace the current logged on user (the one shown in device summary) to the ITSM device list for quick reference like in the old RMM tool.

Hi @indieserve

Thank you for your feedback!