PDA

View Full Version : Get the Windows Login and Logout times



Jeevie
12-22-2011, 07:38 AM
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.

Kenneth Hobs
12-29-2011, 12:58 PM
To be able to see user log on time you have to audit this in "local security (http://www.techsupportforum.com/forums/#) 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/VB-WinAPI/31942873/find-the-login-time-of-the-current-windows-user.aspx

bjoshi
12-30-2011, 06:01 AM
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

Kenneth Hobs
12-30-2011, 07:03 AM
I could show you how to make the API code work but the WMI method is fairly easy to show.

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

bjoshi
12-30-2011, 11:44 PM
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

Kenneth Hobs
12-31-2011, 07:32 AM
http://msdn.microsoft.com/en-us/library/ms974579.aspx

tdevos64
10-19-2017, 04:56 AM
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?

tdevos64
10-19-2017, 04:58 AM
.

Kenneth Hobs
10-19-2017, 07:16 AM
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.

tdevos64
10-19-2017, 08:35 AM
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.

Kenneth Hobs
10-19-2017, 08:50 AM
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.

tdevos64
10-19-2017, 09:17 AM
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,

20699

It runs perfect for myself. When I hardcode another users domain\name (to test) then it comes up with a error as above.

Kenneth Hobs
10-19-2017, 09:58 AM
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/Forums/en-US/0c299a66-0e0a-4f81-9a90-e63da9311659/query-ad-and-get-lastlogontimestamp?forum=vbgeneral

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