Displaying userid of last logged-in user

G

Guest

Guest
Archived from groups: microsoft.public.windowsxp.security_admin (More info?)

How can I determine the userid of the last user account that successfully
logged onto a particular workstation?
 
G

Guest

Guest
Archived from groups: microsoft.public.windowsxp.security_admin (More info?)

Peter Chaves wrote:

> How can I determine the userid of the last user account that
> successfully logged onto a particular workstation?
Hi,

The value data of DefaultUserName under the registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
will indicate who logged on last.


Using a VBScript to obtain the value:

'--------------------8<----------------------

' use "." for local computer
sNode = "some computer name or IP address here"

Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE

On Error Resume Next
Set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& sNode & "/root/default:StdRegProv")

If Err.Number <> 0 Then
On Error Goto 0
WScript.Echo "Could not connect to computer " & sNode
Else
On Error Goto 0
sPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
sValueName = "DefaultUserName"

If oReg.GetStringValue(HKLM, sPath, sValueName, sValue) = 0 Then
WScript.Echo "DefaultUserName from Registry Retrieved Remotely: " & sValue
Else
WScript.Echo "Could not read DefaultUserName from computer " & sNode
End If
End If
'--------------------8<----------------------


WSH 5.6 documentation (local help file) can be downloaded
from here if you haven't got it already:
http://msdn.microsoft.com/downloads/list/webdev.asp




--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx