Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for whohasit.bat

(view source code of whohasit.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4. :: Check command line arguments
  5. IF      "%~1"==""           GOTO Syntax
  6. IF NOT  "%~2"==""           GOTO Syntax
  7. DSGET.EXE   /? >NUL 2>&1 || GOTO Syntax
  8. DSQUERY.EXE /? >NUL 2>&1 || GOTO Syntax
  9. FINDSTR.EXE /? >NUL 2>&1 || GOTO Syntax
  10. ECHO.%~1 | FINDSTR.EXE /R /I /V /C:"[*?&\|\\/:;, ]" >NUL 2>&1 && GOTO Syntax
  11. ECHO.%~1 | FINDSTR.EXE /R /I /C:"[A-Z0-9_-]"        >NUL 2>&1 || GOTO Syntax
  12.  
  13. :: Use local variables
  14. SETLOCAL
  15.  
  16. :: Check if remote PC is on-line
  17. PING %~1 -n 1 -w 2000 2>NUL | FIND "TTL=" >NUL
  18. IF ERRORLEVEL 1 (
  19. 	ECHO Computer %1 is off-line
  20. 	GOTO End
  21. )
  22.  
  23. :: Check logged on user and retrieve display name from Active Directory
  24. SET Descr=
  25. FOR /F %%A IN ('NBTSTAT.EXE -a %~1 ^| FIND.EXE "<03>" ^| FIND.EXE /V /I "%~1"') DO (
  26. 	FOR /F "tokens=*" %%B IN ('DSQUERY.EXE User -samid %%A 2^>NUL ^| DSGET.EXE User -samid -display 2^>NUL ^| FIND.EXE /I "%%A"') DO SET Descr=%%B
  27. )
  28. IF DEFINED Descr (
  29. 	ECHO.%~1	%Descr%
  30. 	GOTO End
  31. )
  32.  
  33. :Syntax
  34. ECHO.
  35. ECHO WhoHasIt.bat,  Version 1.00 for Winows 2000 / XP / Server 2003
  36. ECHO Display the (SAM) user ID and display name for the domain user
  37. ECHO logged on to the specified computer
  38. ECHO.
  39. ECHO Usage:  WHOHASIT  computer_name
  40. ECHO.
  41. ECHO Where:  computer_name  is the host name of the remote
  42. ECHO                        computer to be investigated
  43. ECHO.
  44. ECHO Notes:  Will only work in an Active Directory domain or forest.
  45. ECHO         Requires DSQUERY and DSGET, native tools in Windows Server 2003.
  46. ECHO.
  47. ECHO Written by Rob van der Woude
  48. ECHO http://www.robvanderwoude.com
  49.  
  50. :End
  51. IF "%OS%"=="Windows_NT" ENDLOCAL
  52.  

page last modified: 2024-04-16; loaded in 0.0071 seconds