Question : How to add username of user who executed script to log

Dear experts,

I created a script which unlocks user accounts and writes output to a .txt log:

@echo off

SET netdrive=SAMPLE_NETWORK_DRIVE
pushd %netdrive%
set /p username=Please enter user name:
dsquery user -samid %username% | dsmod user -disabled no
For /f "tokens=4 delims=," %%A in ('dsquery user -samid %username%') do set Department=%%A
echo Account Unlocked,%username%,%Department%,%TIME%,%DATE%>>unlocks.txt
popd

What I also need is to record username of user who ran the script and record it in the log. Is that possible with batch scripting?

I addition I would like to do two extra things:

1. remove the "OU=" part from the log so that output reads %Department% instead of OU=%Department%
2. Only record %Department% information if the user belongs to a specific OU (i.e. IT or Marketing)

Please feel free to share your thoughts.

Thanks in advance,
Skielve

Answer : How to add username of user who executed script to log

That's already in there; the current user's name is in the default environment variable "%username%"; that's why I've changed the "username" you've used before to "user"; in the log line, the first entry is now the name of the user running the script.
Random Solutions  
 
programming4us programming4us