Consulting

Results 1 to 13 of 13

Thread: Get the Windows Login and Logout times

  1. #1

    Smile Get the Windows Login and Logout times

    Hi All

    I am trying to create a piece of routine that would capture the windows login and logout times. Can anyone help me with the code ?

    Appreciate any help.

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    To be able to see user log on time you have to audit this in "local security settings" and than you can check this in the eventviewer.

    If the info from the DOS prompt command of SYSTEMINFO would help, that can be captured.

    For an API method see: http://www.eggheadcafe.com/microsoft...dows-user.aspx
    Last edited by Kenneth Hobs; 12-29-2011 at 01:08 PM.

  3. #3
    VBAX Regular
    Joined
    Jul 2010
    Posts
    24
    Location
    Hi Keneth,

    I've been looking for something similar, can this be done in Excel VBA? I failed to understand the code being discussed on the API method link you have provided.

    Regards,
    bjoshi

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I could show you how to make the API code work but the WMI method is fairly easy to show.

    [VBA]Sub Test()
    MsgBox LastLogin
    End Sub

    Function LastLogin() As Date
    Dim Usr, WMI, Data

    Usr = "'" & Environ$("USERDOMAIN") _
    & "\" & Environ$("USERNAME") & "'"
    Set WMI = GetObject("winmgmts:")
    Set Data = WMI.Get("Win32_NetworkLoginProfile.Name=" & Usr)

    LastLogin = CDate(Format(Left(Data.LastLogon, 8), "####-##-##") _
    & " " & Format(Mid(Data.LastLogon, 9, 6), "##:##:##"))
    End Function[/VBA]

  5. #5
    VBAX Regular
    Joined
    Jul 2010
    Posts
    24
    Location
    Hi Kenneth,

    That was enlightening I have a basic query, where can I get information about objects and properties like

    GetObject("winmgmts:")
    WMI.Get("Win32_NetworkLoginProfile.Name=" & Usr)
    Data.LastLogon

    Best Regards,
    bjoshi

  6. #6

  7. #7
    VBAX Newbie
    Joined
    Oct 2017
    Location
    Birmingham UK
    Posts
    4
    Location
    Kenneth

    I tried to use your script and adapt it a bit to get another users log in date and time (not the one who would run the VBA script you gave). Essentially I can hard code the userdomain and the username in the script. I just need the program to return that time and date for display to the current user.
    How can that be done because if I hard code it, it fails. Any help please?
    If Excel could have made Coffee... it would have!

  8. #8
    VBAX Newbie
    Joined
    Oct 2017
    Location
    Birmingham UK
    Posts
    4
    Location
    .
    Last edited by tdevos64; 10-19-2017 at 05:03 AM. Reason: Duplicated in error

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Welcome to the forum!

    That sort of thing would only be possible for a network administrator. The logon would not be the local computer, ".", but a remote. Network administrators should have tools to do that and more.

  10. #10
    VBAX Newbie
    Joined
    Oct 2017
    Location
    Birmingham UK
    Posts
    4
    Location
    Quote Originally Posted by Kenneth Hobs View Post
    Welcome to the forum!

    That sort of thing would only be possible for a network administrator. The logon would not be the local computer, ".", but a remote. Network administrators should have tools to do that and more.
    Kenneth, thanks for the quick response. I am the head of finance in a small company and I am the IT Administrator as well. What I am trying to do is using Excel VBA to check whether a user is logged into the system as then determines how one of my Excel Dashboards needs to respond. I run several Excel Reports on a dashboard and I need schedule certain actions required within these dashboard sheets depending upon whether a user is logged in or not.

    So the VBA script I need will work within Excel on one and the same server for all users logged into that server.
    We have a central server with MS Terminal services. No user uses a desktop as all clients are "Thin Clients"..... So users do not use remote computers / workstations. Every user logs directly onto the server. We use Server 2008.
    If Excel could have made Coffee... it would have!

  11. #11
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Since I don't have those rights, I can't really test such. Does it run as-is for yourself?

    I find it surprising that this thread has seen so many views after all these years. I am sure many have the same goal as you and some even have your admin rights.

  12. #12
    VBAX Newbie
    Joined
    Oct 2017
    Location
    Birmingham UK
    Posts
    4
    Location
    Quote Originally Posted by Kenneth Hobs View Post
    Since I don't have those rights, I can't really test such. Does it run as-is for yourself?

    I find it surprising that this thread has seen so many views after all these years. I am sure many have the same goal as you and some even have your admin rights.
    Hi Kenneth,

    Capture.JPG

    It runs perfect for myself. When I hardcode another users domain\name (to test) then it comes up with a error as above.
    If Excel could have made Coffee... it would have!

  13. #13
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Being IT, you must know the username so that would not be the problem.

    Your best bet might be to do it in vb.net. Here are some examples: https://social.msdn.microsoft.com/Fo...orum=vbgeneral

    This script approach, if you have Windows Server 2008+, might work for you: https://gallery.technet.microsoft.co...-user-527fe3f3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •