echo %username%,,%computername%,,%date%,, %time%,,logged off>>\\server1\logs$\logoffs.csv
Open in new window
Rem Determine whether the session is a local logon or a RDP session
netstat -an |find "3389" |find /I "established" >C:\Result1.txt
For /F "tokens=3 delims= " %%A IN (C:\Result1.txt) DO SET loc=%%A
if (%loc%)==() set loc=console
REM you could use this next routine to determine the subnet(Branch office location) by grabbing the default gateway
Rem You can modify GW= to fit your environment
Rem Modify IP's below to match your subnet gateways
for /f "tokens=2 delims=:" %%a in ('ipconfig ^| find "Gateway"') DO SET GW=%%a
if %GW%== 10.63.106.2 set GW=Branch1
if %GW%== 10.63.106.130 set GW=Branch2
if %GW%== 10.63.106.162 set GW=Branch3
if %GW%== 10.63.106.194 set GW=Branch4
if %GW%== 10.63.106.226 set GW=Branch5
if %GW%== 10.63.107.2 set GW=Branch6
if %GW%== 10.63.107.34 set GW=Branch7
if %GW%== 10.63.107.66 set GW=Branch8
if %GW%== 10.63.107.98 set GW=Branch9
if %GW%== 10.63.107.130 set GW=Branch10
if %GW%== 10.63.107.162 set GW=Branch11
if %GW%== 10.63.107.194 set GW=Branch12
REM this grabs the computer ip address
for /f "Tokens=2 Delims=" %%i in ('ping -n 1 "%computername%"') do set IP=%%i
Rem used double commas for readability
echo %username%,,%computername%,,%IP%,,%GW%,,%Loc%,,%Date:~0,16%,,%Time:~0,5%,,"logged on" >>\\server1\logs$\logins.csv
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.
Please enter a first name
Please enter a last name
Must be at least 4 characters long.
Join and Comment
From novice to tech pro — start learning today.
Premium members can enroll in this course at no extra cost.