how to get current user or recently logged on user name?

how to get current user or recently logged on user name?

Post by Sm9uYXRoYW » Thu, 13 Jan 2005 10:33:02


hi,

i'm using an sms like product to manage our user machines and need to
execute a script locally on each client. my problem is the the script is
executed under the local system account instead of the user's account and the
script is supposed to return the username of the person currently logged on.
instead of returning something like 'jchan', the script will expectedly
return 'system'.

is there any known workaround to grab the real user logged in (if any) while
the script is running under the system account? i'd even settle for knowing
who was the last person to login on the machine.

here's the code snippet of what i have:
----
Set objNet = CreateObject("WScript.NetWork")
UserName = objNet.UserName
' wscript.echo UserName
----

thanks
 
 
 

how to get current user or recently logged on user name?

Post by Al Dunbar » Thu, 13 Jan 2005 13:26:10


In fact, that is a good thing, as the user account lacks the privs to
install software (or should).

on.
while
knowing

You might be able to deduce the currently logged on use with a WMI script.
IIRC, however, the code to determine who is logged in enumerates all logged
in accounts including service accounts, so you would need to find a way to
identify whch one is the current domain user.

/Al

 
 
 

how to get current user or recently logged on user name?

Post by Pete » Thu, 13 Jan 2005 15:44:51


Hi Jon,

maybe this is not exactly what you are looking for, but recently I was
using "psloggedon" from PsTools on www.sysinternals.com;

Wishing a nice day,

Peter

Olomouc, Czech Rep.
 
 
 

how to get current user or recently logged on user name?

Post by bWRpZGR » Fri, 14 Jan 2005 06:21:02

The script below will use WMI to get you the currently logged on user, but it
requires input of the computername to do it. You can also use WMI to query
the current computername, let me know if you need that also. NOTE: The
newsgroup posting system will wordwrap lines of code so hopefully you know
enough vb to see where the lines are wrapped.


computername = 'put your computer name here

set myobject = getobject("winmgmts:\\" & computername & "\root\cimv2")
set myobject2=myobject.get("win32_computersystem.name='" & computername & "'")
wscript.echo myobject2.username & " is currently logged on to " & computername
 
 
 

how to get current user or recently logged on user name?

Post by MV » Fri, 14 Jan 2005 07:15:15


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.


For Win2k3 Server and Windows XP (as long as the "Terminal Services"
service is not disabled), to obtain the name of the current console
user, an option is to parse the output of Qwinsta.exe (comes builtin
with the OS).

To list the console user, run the following command (to see the output,
run it in a command prompt)

QWINSTA.EXE console /SERVER:%COMPUTERNAME%

(the /SERVER: is not really necessary for local computer)


Here is a VBScript that obtains the console user name and puts it in a
variable:

'--------------------8<----------------------
Set oWshNet = CreateObject("WScript.Network")
' use current computer
sComputerName = oWshNet.ComputerName

sUserName = GetConsoleUser(sComputerName)
If sUserName <> "" Then
WScript.Echo "Console user name: " & sUserName
Else
WScript.Echo "Console user name not found with QWINSTA.EXE"
End If

Function GetConsoleUser(sHost)
' Function will return console user name from QWINSTA.EXE
' Windows XP and Win2k3 Server only

Set oShell = CreateObject("Wscript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")

sTempFile = oFS.GetSpecialFolder(2).ShortPath & "\" & oFS.GetTempName

'Run command via Command Prompt and use for /f to extract user name
'Dump results into a temp text file
oShell.Run "%ComSpec% /c for /f ""skip=1 Tokens=2"" %i in " _
& "('%SystemRoot%\System32\QWINSTA.EXE console /SERVER:" _
& sHost & "') do echo %i >" & sTempFile, 0, True

GetConsoleUser = "" 'init value

If oFS.FileExists(sTempFile) Then
'Open the temp Text File and Read out the Data
Set oTF = oFS.OpenTextFile(sTempFile)

'Parse the text file
Do While Not oTF.AtEndOfStream
GetConsoleUser = Trim(oTF.ReadLine)
Loop

'Close it
oTF.Close
'Delete It
oFS.DeleteFile sTempFile
End If

End Function
'--------------------8<----------------------

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

how to get current user or recently logged on user name?

Post by Al Dunbar » Fri, 14 Jan 2005 09:38:24


it
query

If the script is running on the computer you want the info from, then try
this:

With CreateObject( "WScript.Network" )
computername = .computername
End With

/Al

"'")
computername


is
and the
logged on.
expectedly
while
knowing