Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get user's login name?

Status
Not open for further replies.
May 24, 2006
219
0
0
US
We support many networks remotely, and we frequently encounter situations where we know a Machine Name (i.e. Control Panel > System > Network Indentification), but we need to be able to determine who's machine that is...

that is, when we look in Network Neighborhood, we see lots of machines, but don't have any idea of who's they are.

Is there a utility that will provide this information for us? Or is there a Group Policy setting or some kind of log we can review to determine who was the most recent person to login to a given machine?
 
You can always create a logon script that basically echoes that info to a text file, as a last resort, if necessary:

echo %USERNAME% has logged onto %COMPUTERNAME% at %DATE%, %TIME%. >> \\MyServer\NETLOGON\TIMESTAMPS.TXT

Simple, yet straightforward...
 
Porkchopexpress, thanks for the link to pstools... we've used their tools before and they are excellent. We're also familiar with psloggedon, but unfortunately, it doesn't include the machine name in its output.
 
CathodeRay said:
or some kind of log we can review to determine who was the most recent person to login to a given machine

How about the security log of the machine, if you are logging that information (success/failure)....
 
Here is a VBScript that will do what your asking, I just threw it together but it works great.
How to use it:
1. Copy the code below into notepad (with word wrap disabled).
2. save it as any name with the .vbs extention. Eg. LoggedOnUsers.vbs
2. Create a text file on the root of your C: drive called Computers.txt
3. In the Computers.txt file, enter the name of each machine you wish to check. One machine per-line as the script uses this text file to create an array. The array is delimited by a carriage return, so it Should look like the following:
ComputerName1
ComputerName2
ComputerName3
and so on...

4. you can place the script where ever you like, for this example it is on the root to C:
5. Open a commmand promt and type the following command:
c:\cscript LoggedOnUsers.vbs > LoggedOnUsers.txt
This command runs the script and send the output to the LoggedOnUsers.txt file, this is where you will find the results.

Results will look similar to the following:
UserName: Domain\Username is logged in at computer ComputerName1
===========================================================================
ComputerName2, The remote server machine does not exist or is unavailable
===========================================================================
No User is logged on system: ComputerName3
===========================================================================

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\Computers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputers = Split(strText, VbCrLf)

For Each strComputer In arrComputers
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    	If Err <> 0 Then
			WScript.Echo strComputer & ", " & Err.Description
			WScript.Echo "==========================================================================="
			Err.Clear
		Else
    		Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
        	For Each objItem in colItems
        	If objItem.Username <> "" Then
        	WScript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
        	WScript.Echo "==========================================================================="
        	Else
			WScript.Echo "No User is logged on system: " & strcomputer
        	WScript.Echo "==========================================================================="        	
        	End If
    		Next
    	End If
    Set objItem = Nothing: Set colItems = Nothing:
    Set objWMIService = Nothing
Next
 
Mistake on Script above. It didn't copy the first two lines:
On Error Resume Next
Const ForReading = 1
Won't do any harm just give you a runtime error.
Use this code instead...
Code:
On Error Resume Next
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("c:\Computers.txt", ForReading)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputers = Split(strText, VbCrLf)

For Each strComputer In arrComputers
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    	If Err <> 0 Then
			WScript.Echo strComputer & ", " & Err.Description
			WScript.Echo "==========================================================================="
			Err.Clear
		Else
    		Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
        	For Each objItem in colItems
        	If objItem.Username <> "" Then
        	WScript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
        	WScript.Echo "==========================================================================="
        	Else
			WScript.Echo "No User is logged on system: " & strcomputer
        	WScript.Echo "==========================================================================="        	
        	End If
    		Next
    	End If
    Set objItem = Nothing: Set colItems = Nothing:
    Set objWMIService = Nothing
Next
 
Similar idea here but this one logs to an access db, this does require JET to be installed on the client though.

Code:
On Error Resume Next

Dim adoCn
Dim adoRs
Dim network
Dim user
Dim compname
Dim strSQLInsert


Set network = CreateObject("Wscript.Network")
user = network.username
compname = network.computername

Set adoCn = CreateObject("ADODB.Connection")

adoCn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
           "Data Source=\\server\logs$\log.mdb" 'CHANGE THIS BIT

'Check the connection opened ok           
If Err.Number <> 0 Then           
	Call ErrHandler
End If


strSQLInsert = "INSERT INTO [Log On] ([date], [time], [user], compname) " & _  
	"VALUES ('" & Date & "', '" & Time & "', '" & user & "', '" & compname & "')"

adoCn.Execute strSQLInsert, , 8

'Check the data was inserted OK
If Err.Number <> 0 Then           
	Call ErrHandler
End If

adoCn.Close

Set adoCn = Nothing
Set network = Nothing


Sub ErrHandler()
Dim fso, f

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile("\\server\logs$\" & LogError & ".txt" , ForAppending, True)

f.WriteLine Date & ", " & Time & ", " & user & ", " & compname & ", " & Chr(34) & Err.Description & Chr(34)
f.Close

Set fso = nothing

Err.Clear

End Sub
 
GVN, we implemented your solution, and it works *pretty* well... but it doesn't seem to be logging in everybody's activity. Is there something special we need to do in the "script" area?

We go into Programs > Administrative Tools > Active Directory Users and Groups, and do a right mouse click on the top level (the domain), choose Group Policy > Edit and drill down to User Configuration > Windows Settings > Scripts and edit the properties of "Logon." We browsed out to our batch file with your command in it, then OK, Ok, OK.

Then we made sure the EVERYBODY has permissions on the txt file and folder it's in. When we log in on the domain controller, or another server, it works, but after a full day we don't see ANY other login records... and I'm very sure others must've logged into their workstations using their domain login.

I added another batch file that simply does a NET USE, and it is similarly NOT being executed by almost any of the users. I think I'm missing something in the login script area. Any ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top