forked from bmrf/standalone_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhosOn.bat
108 lines (92 loc) · 3.89 KB
/
WhosOn.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
:: Purpose: Checks who's logged-on to a remote computer
:: Pings first to create an ARP entry, seems to lessen the chance of a timeout error.
:: Requirements: Run this script with a network admin account
:: Author: vocatus on reddit.com/r/sysadmin ( [email protected] ) // PGP key ID: 0x07d1490f82a211a2
:: History: 1.7.1 / Minor header cleanup; Variables section now above prep and checks
:: 1.7.0 * Overhauled Date/Time conversion so we can handle ALL versions of Windows using ANY local date-time format
:: 1.6.0 + Added "SCRIPT_UPDATED" variable to note when the script was last updated
:: / Structure cleanup
:: 1.5.0 / Comment cleanup
:: / Structure cleanup
:: 1.4.0 + Added missing quotes around %TARGET% variable
:: 1.3.0 + Added /FAILFAST:on to WMIC method to allow faster time-outs if the machine isn't responding
:: 1.2.0 + Added "WMIC /NODE:%TARGET% csproduct get vendor,name,identifyingnumber" to WMIC section
:: 1.1.0 + Added two additional methods for checking which can be activated by uncommenting
:: + Added METHOD variable to reflect which method we're trying
:: / Capped variables properly
:: 1.0.0 Initial write
SETLOCAL
@echo off
:::::::::::::::
:: VARIABLES :: -------------- These are the defaults. Change them if you so desire. --------- ::
:::::::::::::::
:: Set method of querying remote system. Valid methods are:
:: - WMIC (most reliable, however provides the least information)
:: - Windows_query_user (requires Windows Vista and up; doesn't work on XP)
:: - PsLoggedon (requires PsLoggedon.exe from sysinternals)
set METHOD=WMIC
:: --------------------------- Don't edit anything below this line --------------------------- ::
:::::::::::::::::::::
:: PREP AND CHECKS ::
:::::::::::::::::::::
@echo off
title Who's Logged On?
set TARGET=%1%
set SCRIPT_VERSION=1.7.1
set SCRIPT_UPDATED=2014-09-08
set RUNONCE=NO
:: Get the date into ISO 8601 standard date format (yyyy-mm-dd) so we can use it
FOR /f %%a in ('WMIC OS GET LocalDateTime ^| find "."') DO set DTS=%%a
set CUR_DATE=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%
:::::::::::::::
:: EXECUTION ::
:::::::::::::::
if not '%1%'=='' set RUNONCE=YES && goto run_once
cls
echo.
echo Check who's logged on to a remote computer.
echo.
echo Available methods, from most reliable to least:
echo - WMIC Uses WMIC to query the remote system
echo - Windows_query_user Uses Windows' built-in "query user /SERVER:%TARGET%" command
echo (Your local PC must be running Windows Vista or 7 to use this)
echo - PsLoggedon Uses SysInternal's PsLoggedon.exe to query the remote system
echo.
echo Currently set to use the '%METHOD%' method.
echo.
:loop
set /P TARGET= Check:
if %TARGET%==exit goto end
:go
echo.
echo Checking %TARGET% using %METHOD%...
echo.
ping -n 1 %TARGET% >NUL
goto %METHOD%
::
:: Method #1: WMIC. Most accurate, usually works. Valid on XP and up.
::
:WMIC
WMIC /FAILFAST:on /NODE:"%TARGET%" computersystem GET name, username
::WMIC /FAILFAST:on /NODE:"%TARGET%" csproduct get vendor,name,identifyingnumber
if %ERRORLEVEL%==1 echo. && echo WMIC method failed. Trying Windows_query_user method... && echo. && goto Windows_query_user
if %RUNONCE%==YES (goto end) ELSE (goto loop)
::
:: Method #2: Windows built-in. Only available on Vista/7 to my knowledge. Provides the most information.
::
:Windows_query_user
query user /SERVER:%TARGET%
if %RUNONCE%==YES (goto end) ELSE (goto loop)
::
:: Method #3: SysInternals utility. Works most of the time. the PsLoggedon.exe file is required.
::
:PsLoggedon
PsLoggedon.exe -l \\%TARGET%
if %RUNONCE%==YES goto end
:run_once
echo.
echo Checking %TARGET% using %METHOD% method...
echo.
goto %METHOD%
:end
title %USERNAME%