Solved

Batch FIle: Report back User logged in and PC name

Posted on 2010-08-24
15
465 Views
Last Modified: 2012-05-11
I am looking to create a batch file which will go out to all the computers on my LAN and report back a log file of the user logged in and the computer name.  How can I achieve this?

All machines are Windows XP PRO, SP2, SP3.

Thanks!
0
Comment
Question by:SgtofMarines
[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
  • 6
  • 4
  • 4
  • +1
15 Comments
 
LVL 5

Expert Comment

by:RiaanRoux
ID: 33511122
You could pipe them to a file using the following commands:

@echo off
echo %computername% > %computername%.txt
echo %username% >> %computername%.txt

You could then just copy the file to a mapped drive using the same batch file.

ie.

copy %computername%.txt \\servername\path\
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 167 total points
ID: 33511782
If you create a TXT file of the computers you want to query, then you can use the Microsoft PSLOGGEDON free tool from:

http://technet.microsoft.com/en-us/sysinternals/bb897545.aspx

and build a small script something like this to check each computer:

@echo off
if exist "user.txt" del "user.txt"
for /F "usebackq" %%A in ("computers.txt") do psloggedon -l -x \\%%A>>"user.txt"

~bp
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33511999
Are you looking for something like this in a text file:

Computer Name                                       User
--------------------                                     --------------------
Computer1                                              User99
Computer 2                                             User57
Computer 14                                           User83

You could add a section to your login script to accomplish this.

echo %computername%                                        %username% >> yourfile.txt

Use the full path to where you placed the file you created for yourfile.txt

This will get all computers as they login but will not get any that are not.

Start by creating a file, then
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:SgtofMarines
ID: 33512297
Thanks Bill and Pony.

Bill I am going to try your pstools suggestion, I do have that installed and will run the command in a few.

Pony, your suggestion also looks clean, the only thing, I have to run this local from my laptop.  I am not allowed to touch the domain login script we currently have.  I need something I can run locally from my laptop that can report back Domain Computer Names and the Domain UserID that is logged into it.

Example:

Computer1    jblow
Computer2    jdoe
Computer3    jjohnson  

Thanks Guys

0
 
LVL 26

Expert Comment

by:pony10us
ID: 33512360
bill

What if you don't know the names of all the computers? What if someone has connected a computer that you are unaware of? Is there a way to determine that using a CLI?
0
 

Author Comment

by:SgtofMarines
ID: 33512480
Bill, your script would work if it would opened up the registry.  I got a registry error has occured when I run it against other computers, but when I ran it against mines, it provided the information I was looking for.  Any way to force my user account/password to open up hosts registries?

error: Connecting to Registry of \\G5cn2f1...
                                                                             
Error opening HKEY_USERS for \\G5cn2f1
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33512813
I suspect you need to have admin rights on the remote PCs.

~bp
0
 

Author Comment

by:SgtofMarines
ID: 33513319
I am a member of a group which is a member of the local admin group
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33513485
I have been able to get the username with this but I can't seem to get the computer name into the user.txt file.
@echo off
setlocal EnableDelayedExpansion

if exist "user.txt" del "user.txt"
for /F "usebackq" %%A in ("computers.txt") do (

wmic /node:%%A computersystem get username >> user.txt

)

Open in new window

0
 
LVL 26

Expert Comment

by:pony10us
ID: 33513986
I can add:    echo %%A >> user.txt at line 3 but then the results of line 7 get all distorted.

ibmmjnz447  
U s e r N a m e                  
 
 H A Y D E N \ s s c a r n a h    
 
 
0
 
LVL 26

Assisted Solution

by:pony10us
pony10us earned 83 total points
ID: 33515340
Here is a little more involved wscript that a coworker of mine just happened to have.  
Option Explicit
Dim oFSO, sFile, oFile, sText
Dim filesys, filetxt
Dim objWMIService, objComputer, colComputer, strComputer 
dim objPing, objStatus

Set oFSO = CreateObject("Scripting.FileSystemObject")
sFile = "hostnames.txt"
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
 Do While Not oFile.AtEndOfStream
  sText = oFile.ReadLine
   If Trim(sText) <> "" Then
	strComputer = sText
	'******************** Ping computer, if not up go to next name in list *****************
	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
	ExecQuery("select Replysize from Win32_PingStatus where address = '" & strComputer & "'")
	For Each objStatus in objPing
	 If  IsNull(objStatus.ReplySize) Then
 	   'WScript.Echo "computer is offline"
	 Else
 	   'WScript.Echo "computer is online"
	   Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
	   Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem") 
	   For Each objComputer in colComputer      
	      '******************Write Hostname and User to File********************
	      Set filesys = CreateObject("Scripting.FileSystemObject")
	      Set filetxt = filesys.OpenTextFile("status.txt", 8, True) 
	      filetxt.WriteLine(objComputer.Username & " " & strComputer)
	      filetxt.Close 
 Next

 	End If
	Next
	Set objPing=Nothing
	Set objStatus=Nothing
   End If
 Loop
oFile.Close
Else
WScript.Echo "File containing hostnames was not found."
End If
Wscript.Echo "Check of logged on user is complete."

Open in new window

0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 167 total points
ID: 33515512
Give this a try from the command line and see if it's close to what you want:

wmic /node:@computers.txt computersystem get username /format:csv

~bp
0
 
LVL 26

Expert Comment

by:pony10us
ID: 33515924
bill

Much nicer format than what I had as usuall.   :)

0
 

Author Comment

by:SgtofMarines
ID: 33516042
Ok Gents,

I will try these new items in the morning.  Appreciate it
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 33516787
And naturally if that works for you just pipe it's output to a file...

wmic /node:@computers.txt computersystem get username /format:csv >users.csv

~bp
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Sometimes people don't understand why download speed shows differently for Windows than Linux.Specially, this article covers and shows the solution for throughput difference for Windows than a Linux machine. For this, I arranged a test scenario.I…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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