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

Find which user has presently logged in a machine.

Hi,

I want a script that will ask me for a machine names and when i give the machine name it tells me who has logged in at present.In the same script need to have a way to scan the whole file which has the machine names and tell me who  is using which machine.

Regards
Sharath
0
bsharath
Asked:
bsharath
1 Solution
 
Farhan KaziSystems EngineerCommented:
:: ===============
:: READ THIS FIRST
:: ===============
:: * To run this script you must have domain administrators rights.
:: * This script require "PSLoggedOn.exe" file (comes with PSTools) on C: drive root
:: * Successful run will generate "LoginReport.txt" file on C: drive root.
:: * Copy and paste following script in notepad and save it with any name having .cmd extension.
:: *** SCRIPT START ***
@Echo OFF
SETLOCAL EnableDelayedExpansion

IF EXIST C:\LoginReport.txt DEL /F /Q C:\LoginReport.txt

SET /P SysName=Enter single system name or a file having computers name listed:
Echo %SysName% | FIND "." >NUL
Echo %SysName%
IF NOT ErrorLevel 1 (Call :FindMultiple) ELSE (Call :FindSigle)
Goto :EndScript
:FindSigle

PING -n 1 -w 1000 %SysName%|Find /I "TTL" >NUL
IF NOT ErrorLevel 1 (
      Echo ------------ >>C:\LoginReport.txt
      Echo %SysName% >>C:\LoginReport.txt
      Echo ------------ >>C:\LoginReport.txt
      PSLoggedOn \\%SysName% -l | Find "/" >>C:\LoginReport.txt
)ELSE (            
      Echo ------------ >>C:\LoginReport.txt
      Echo %SysName%: Not able to connect >>C:\LoginReport.txt
      Echo ------------ >>C:\LoginReport.txt)
Goto :EndScript

:FindMultiple
IF NOT EXIST %SysName% Goto ShowErr
FOR %%R IN (%SysName%) Do IF %%~zR EQU 0 Goto ShowErr
FOR /F %%c IN ('Type %SysName%') Do (
    Echo Processing: %%c
      PING -n 1 -w 1000 %%c|Find /I "TTL" >NUL
      IF NOT ErrorLevel 1 (
            Echo ------------ >>C:\LoginReport.txt
            Echo %%c: >>C:\LoginReport.txt
            Echo ------------ >>C:\LoginReport.txt
            PSLoggedOn \\%%c -l | Find "/" >>C:\LoginReport.txt
            Echo.>>C:\LoginReport.txt
      )ELSE (
            Echo ------------ >>C:\LoginReport.txt
            Echo %%c: Not able to connect >>C:\LoginReport.txt
            Echo ------------ >>C:\LoginReport.txt
            Echo.>>C:\LoginReport.txt)
)      

Goto EndScript
:ShowErr
Echo "%SysName%" file does not exist or file is empty!
:EndScript
ENDLOCAL
:: Batch Script End
0
 
canaliCommented:
: ================
:: READ THIS FIRST
:: ================
:: * To run this script you must have domain administrators rights.
:: *** Need psloggedon,exe in c:\ (get it from http://www.microsoft.com/technet/sysinternals/utilities/psloggedon.mspx)
:: * Successful run will display logged on sigle machine or on  all computers.
:: * This script require "Computers.txt" file on C: drive root from where it will pick computer names.
:: * Copy and Paste following script into notepad and save it with any name having .bat extension.
:: Batch Script Start

@Echo Off
SETLOCAL EnableDelayedExpansion
Echo.
Echo.
Echo Search logged user on pcSingle or pcList ^?    
Set /p pc="[MachineName or L]? "
if +%pc%+==++ Goto ShowErr
if /i +%pc%+==+L+ Goto List
if /i not +%pc%+==+L+ c:\pro\psloggedon.exe \\%pc%|findstr /r /v "^NT" |find /i /v  "error"
Goto EndScript
:List
IF NOT EXIST C:\Computers.txt Goto ShowErr
FOR %%R IN (C:\Computers.txt) Do IF %%~zR EQU 0 Goto ShowErr
IF EXIST C:\userlogged.txt DEL /F /Q C:\userlogged.txt

FOR /F %%c IN ('Type Computers.txt') Do (
    Echo Processing: %%c
      PING -n 1 -w 1000 %%c|Find /I "TTL" >NUL
      IF NOT ErrorLevel 1 (
            c:\pro\psloggedon.exe \\%%c |findstr /r /v "^NT" |find /i /v  "error"
      )ELSE (Echo %%c: Not able to connect )
)      

Goto EndScript
:ShowErr
Echo.
Echo --- ERROR ---
if +%pc%+==++ (Echo Nothing entered^!^! I Need a machineName or L
 )else Echo C:\Computers.txt file does not exist or file is empty!
:EndScript
ENDLOCAL
:: Batch Script End
0
 
captainCommented:
Use AD auditing, see KB for details:
http://support.microsoft.com/?kbid=314955

hth
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
bsharathAuthor Commented:
Nice one Farhan...
0
 
bsharathAuthor Commented:
Oops sorry closed it before i could see others answers....
0
 
canaliCommented:
no problem! I saw after Farhan answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now