Getting a Script to Append to an Excel or CSV File

How would I rewrite this code to append the results of just the users found as local admins in the result - any result with just 4 alphanumeric characters that would where ABCD.TXT would be created with the machine name in the file, for example, along with the machine name into a CSV File on a Share located on a server?

Example:

UserName, MachineName
ABCD,LAPTOP1
EFHG,LAPTOP2
12DS,LAPTOP3
321A,LAPTOP4

echo *** Checking Admin rights of PC and User ***
net localgroup administrators | find "\" | find /v "Domain Admins" > \\server\share\admins\PC\%COMPUTERNAME%.txt
for /F "tokens=1,2 delims=\" %%i in (\\server\share\admins\PC\%COMPUTERNAME%.txt) do if not "%%j"=="" echo %DATE% -- %COMPUTERNAME% >> \\server\share\admins\USER\%%j.TXT
TechnologyManguAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITCommented:
> ...append the results of just the users found as local admins in the result - any result with just 4 alphanumeric characters that would where ABCD.TXT would be created with the machine name in the file

You're saying several things here. Please clarify.
0
NVITCommented:
This would give the first 4 letters of username:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

for /F "tokens=1-2 delims=\" %%i in (\\server\share\admins\PC\%COMPUTERNAME%.txt) do (
  set un=%%j
  echo %DATE% -- !un:~0,4!,%COMPUTERNAME%
)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TechnologyManguAuthor Commented:
The script I included creates a number of files:

echo *** Checking Admin rights of PC and User ***
net localgroup administrators | find "\" | find /v "Domain Admins" > \\server\share\admins\PC\%COMPUTERNAME%.txt
for /F "tokens=1,2 delims=\" %%i in (\\server\share\admins\PC\%COMPUTERNAME%.txt) do if not "%%j"=="" echo %DATE% -- %COMPUTERNAME% >> \\server\share\admins\USER\%%j.TXT

The %COMPUTERNAME%.txt file is created and for all entries in the local administrators group it finds everything that does not match "Domain Admins" and then creates a USER file (%%j.TXT") for each user with an indication in the file as to which machine the user is an admin on.

What I'd like is for the script to append to a CSV file a line for each user that is included in the localgroup administrators using the < /C "%USERNAME%" > setting that shows the username and computer for each computer where the %USERNAME% shows up in the local administrators group.

I hope this clarifies.
0
NVITCommented:
Please clarify...
>  ... any result with just 4 alphanumeric characters that would where ABCD.TXT

> ... using the < /C "%USERNAME%" > setting
0
oBdACommented:
This should do what you want.
You can run it remotely against a set of machines; using a local script to a shared csv file create errors when two scripts try to add to the csv at the same time.
You can either run it without argument, in which case it will just ask for a single computer name and write no file, or you can pass it a file with computer names as argument. The script will then process all computers and generate a report with the name of the computer file and "_LocalAdmins.csv" attached at the end.
@echo off
setlocal enabledelayedexpansion
set ExcludeNames="Domain Admins" "Administrator"
set CsvHeader="Computer","Name","Error"

set TempWmiFile=%Temp%\wmic.tmp
set TempErrFile=%Temp%\wmic_err.tmp

if "%~1"=="" (
	set Computer=
	set /p Computer=Computer name: 
	if "!Computer!"=="" goto :eof
	call :Process !Computer!
	pause
	goto :eof
)
if not exist "%~1" (
	echo Server file '%~1' not found.
	pause
	goto :eof
)
set ServerFile=%~f1
set CsvFile=%~dpn1_LocalAdmins.csv

if defined CsvFile >"%CsvFile%" echo %CsvHeader%

for /f %%a in ('type "%ServerFile%"') do (
	call :Process %%a
)
echo Done.
pause
goto :eof

:Process <Computer>
set Computer=%~1
echo Processing %Computer% ...
ping.exe -4 -n 2 %Computer% | find.exe /i "TTL" >NUL
if errorlevel 1 (
	echo ... offline.
	if defined CsvFile >>"%CsvFile%" echo %Computer%,"n/a","Offline"
	goto :eof
)
wmic.exe /Node:%Computer% /FailFast:ON Path Win32_GroupUser Where (GroupComponent="Win32_Group.Name=\"Administrators\",Domain=\"%Computer%\"") Get PartComponent 1>"%TempWmiFile%" 2>"%TempErrFile%"
for %%e in ("%TempErrFile%") do if %%~ze gtr 0 (
	for /f "delims=" %%a in ('type "%TempErrFile%"') do set Error=%%a
	echo ... !Error!.
	if defined CsvFile >>"%CsvFile%" echo %Computer%,"n/a","!Error!"
	goto :eof
)
for /f "tokens=2,4 delims=,=" %%a in ('type "%TempWmiFile%"') do (
	set Domain=%%~a
	call :TrimName %%b
	set Exclude=FALSE
	for %%e in (%ExcludeNames%) do if /i "%%~e"=="!Name!" set Exclude=TRUE
	if "!Exclude!"=="FALSE"	(
		echo     !Domain!\!Name!
		if defined CsvFile >>"%CsvFile%" echo %Computer%,"!Domain!\!Name!",""
	)
)
goto :eof

:TrimName
set Name=%*
set Name=%Name:"=%
goto :eof

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

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.