Solved

Batch FIle: Report back User logged in and PC name

Posted on 2010-08-24
15
462 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
  • 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 52

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
 

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 52

Expert Comment

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

~bp
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 52

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 52

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now