• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

Batch FIle: Report back User logged in and PC name

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
SgtofMarines
Asked:
SgtofMarines
  • 6
  • 4
  • 4
  • +1
3 Solutions
 
RiaanRouxCommented:
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
 
Bill PrewCommented:
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
 
pony10usCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SgtofMarinesAuthor Commented:
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
 
pony10usCommented:
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
 
SgtofMarinesAuthor Commented:
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
 
Bill PrewCommented:
I suspect you need to have admin rights on the remote PCs.

~bp
0
 
SgtofMarinesAuthor Commented:
I am a member of a group which is a member of the local admin group
0
 
pony10usCommented:
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
 
pony10usCommented:
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
 
pony10usCommented:
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
 
Bill PrewCommented:
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
 
pony10usCommented:
bill

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

0
 
SgtofMarinesAuthor Commented:
Ok Gents,

I will try these new items in the morning.  Appreciate it
0
 
Bill PrewCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now