Solved

Script to Display Last Login time

Posted on 2009-03-31
5
1,335 Views
Last Modified: 2012-05-06
Hello,

We have a few windows 2003 terminal servers on a windows 2003 domain.  We want either a software widget or some way that will display a pop up for users when they log on that shows when the last time they logged in.  We were given the following script which will run and display the last login time.  Here is the problem.  If we put this script in the startup folder, they have to log into the terminal server to get the script to run so it will tell them there last login time was a few seconds ago (which is correct, but not what we need).  I dont know if there is a way to display the 2nd to last login or a better way to run the script, but any help would be appreciated. Script is attached.  Thanks.
On Error Resume Next
Dim User
Dim UserName
Dim UserDomain
Dim WshNetwork
Set WshNetwork = CreateObject("WScript.Network")
Set objDomain = getObject("LDAP://rootDse")
UserDomain = objDomain.Get("dnsHostName")
UserName = WshNetwork.UserName
Set User = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
MsgBox "The last time " & UserName & " logged on was: " & vbCRLf & vbCRLf & User.LastLogin

Open in new window

0
Comment
Question by:delmarvamonkey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
Justin_W_Chandler earned 500 total points
ID: 24030456
Here you go! It is presently set up to display the last logon for a fixed number of seconds. You can specify how long it displays in const_displayDelay. Alternatively, it can be set to just display a window with the information and stay open until a key is pressed or the X is clicked. I can add this for you, as it's a pretty minor addition.

Justin Chandler
@echo off
 
:: set the folder for the logs to be stored here
set const_logFolder=c:\users.log
 
:: set the number of seconds to display the last logon here
set const_displayDelay=5
 
title Last logon
mode con cols=22 lines=4
color 17
if not exist "%const_logFolder%" call :proc_makeLogFolder
echo.
if exist "%const_logFolder%\%username%.log" type "%const_logFolder%\%username%.log"
ping -n %const_displayDelay% 127.0.0.1>nul
echo  Your last logon was:>"%const_logFolder%\%username%.log"
echo    %date:~4% %time:~0,-6%>>"%const_logFolder%\%username%.log"
goto :eof
 
 
:proc_makeLogFolder
md "%const_logFolder%"
attrib "%const_logFolder%" +h
goto :eof

Open in new window

24281518---Display-last-logon-ti.txt
0
 

Author Comment

by:delmarvamonkey
ID: 24031070
I get an error when I run this script.

Windows Script Host
Line: 3
Char: 12
Error: Expected '='
Code: 800A0F3
Source: Microsoft VBScript compilation error
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24031302
This is not a VBScript... it's a batch script. Just rename the attached file to LOGONTIME.CMD and put it in your startup folder.
0
 

Author Comment

by:delmarvamonkey
ID: 24031441
Doh!  This will handle multiple users logging into the same terminal server, correct?  It looks like it.
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24031604
Absolutely it will.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question