Solved

Batch FIle: Report back User logged in and PC name

Posted on 2010-08-24
15
461 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 51

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 51

Expert Comment

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

~bp
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 51

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 51

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

14 Experts available now in Live!

Get 1:1 Help Now