batch file is not releasing my variable

batch file is not releasing my variable

The issue I am havening is

I have a batch that will go through a list of PC’s.  Find the current user and then display the sid for that user.  It only works for the first one however.
The very first username and sid it will get.  After that it keeps getting the correct username but when it come time to get the SID it always get the username ands sid of wehatever the first one it pulled was.
Below is my file perhaps you can see where I made an error or what I have missed.
I have highlighted where I believe the error lies but I am not 100% sure.



Thanks so much


rem ----------------------------------------------Start folder redirect fix---------------------------------
rem ----------------------------------------------Start folder redirect fix---------------------------------
@echo off
cls
MODE CON: COLS=150 LINES=70


set /A Counter=0
for /f %%i in (c:\SCCM-Management\tools\computers.txt) do (
  set /A counter+=1
)
cls


echo.
Echo Folder redirect Fix uses c:\SCCM-Management\tools\computers.txt
Echo c:\SCCM-Management\tools\computers.txt has %counter% PC(s) in the list.
Echo.
Echo you are running this as %username%
echo.
Pause


if exist c:\temp\sid.txt del /f /q c:\temp\sid.txt


if exist c:\temp\userslist.txt del /f /q c:\temp\userslist.txt
if exist c:\temp\USERSoffline.txt del /f /q c:\temp\USERSoffline.txt


set /A mynumber=1
SETLOCAL EnableDelayedExpansion
for /f %%i in (c:\SCCM-Management\tools\computers.txt) do (
   ping -n 2 %%i | find /i "bytes=" > nul
   if ErrorLevel 1 (
     echo %%i>>c:\temp\USERSoffline.txt & @echo %%i is not online & set /A mynumber+=1
  ) else (
echo -------------------------
echo -------------------------
echo %%i
Echo Running Task on %%mynumber%% of %Counter% computers
set /A mynumber+=1
echo -------------------------
echo -------------------------
echo %%i>>c:\temp\userslist.txt
echo ** >>c:\temp\userslist.txt
echo.
    echo Current user is:
     for /f "tokens=2 delims=\" %%a in (
       'wmic /node:"%%i" computersystem get username') do (
       echo %%a
       set thisuser=%%a
   )
   echo this user is !thisuser!


echo.
echo.
echo.

echo.>>c:\temp\userslist.txt
echo Current user is:>>c:\temp\userslist.txt
rem for /f "tokens=2 delims=\" %%a in ('wmic /node:%%i computersystem get username') do echo %%a>>c:\temp\userslist.txt


for /F "usebackq delims== skip=1" %%i in (`psgetsid.exe CMR\!thisuser!`) do set usersid=%%i


echo User %thisuser% has a sid of %usersid%
echo User %thisuser% has a sid of %usersid%>>c:\temp\userslist.txt
echo.
Echo %thisuser% has there documents redirected to
reg query "\\%%i\HKU\%usersid%\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal



echo **************************>>c:\temp\userslist.txt
echo.>>c:\temp\userslist.txt
echo.>>c:\temp\userslist.txt

echo.
echo.
del /f /q c:\temp\sid.txt
   )
 )


echo.
echo Report is saved to c:\temp\userslist.txt
echo Missed was saved to c:\temp\USERSoffline.txt
SLPowersEngineerAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Just noticed two more things (sorry, can't test this at the moment):
Purely cosmetic: line 30 should of course be
		echo Running Task on !mynumber! of %Counter% computers

Open in new window

Counter doesn't need delayed expansion, that's set before the loop and never changed.

And in line 49 (and 48, if you reactivate that at some point), you should use another "for /f" variable, %%i is already used for your outer loop.
		for /F "skip=1 usebackq delims== " %%p in (`psgetsid.exe CMR\!thisuser!`) do set usersid=%%p

Open in new window

0
 
oBdACommented:
The batch interpreter reads all statements inside brackets as a single line and expands any variable enclosed in % signs only at that single moment. To expand the variables dynamically while in a loop, you have to enable delayed expansion and use "!" to expand them. You didn't do this with %thisuser% or %usersid%.
Try this (untested):
@echo off
SETLOCAL EnableDelayedExpansion
rem ----------------------------------------------Start folder redirect fix---------------------------------
cls
MODE CON: COLS=150 LINES=70
set /A Counter=0
for /f %%i in (c:\SCCM-Management\tools\computers.txt) do (
	set /A counter+=1
)
echo.
Echo Folder redirect Fix uses c:\SCCM-Management\tools\computers.txt
Echo c:\SCCM-Management\tools\computers.txt has %counter% PC(s) in the list.
Echo.
Echo you are running this as %username%
echo.
Pause
if exist c:\temp\sid.txt del /f /q c:\temp\sid.txt
if exist c:\temp\userslist.txt del /f /q c:\temp\userslist.txt
if exist c:\temp\USERSoffline.txt del /f /q c:\temp\USERSoffline.txt

set /A mynumber=1
for /f %%i in (c:\SCCM-Management\tools\computers.txt) do (
	ping -n 2 %%i | find /i "TTL" > nul
	if ErrorLevel 1 (
		echo %%i>>c:\temp\USERSoffline.txt & @echo %%i is not online & set /A mynumber+=1
	) else (
		echo -------------------------
		echo -------------------------
		echo %%i
		echo Running Task on %%mynumber%% of %Counter% computers
		set /A mynumber+=1
		echo -------------------------
		echo -------------------------
		echo %%i>>c:\temp\userslist.txt
		echo ** >>c:\temp\userslist.txt
		echo.
		echo Current user is:
		for /f "tokens=2 delims=\" %%a in ('wmic /node:"%%i" computersystem get username') do (
			echo %%a
			set thisuser=%%a
		)
		echo this user is !thisuser!
		echo.
		echo.
		echo.
		echo.>>c:\temp\userslist.txt
		echo Current user is:>>c:\temp\userslist.txt
		rem for /f "tokens=2 delims=\" %%a in ('wmic /node:%%i computersystem get username') do echo %%a>>c:\temp\userslist.txt
		for /F "skip=1 usebackq delims== " %%i in (`psgetsid.exe CMR\!thisuser!`) do set usersid=%%i
		echo User !thisuser! has a sid of !usersid!
		echo User !thisuser! has a sid of !usersid!>>c:\temp\userslist.txt
		echo.
		echo **************************>>c:\temp\userslist.txt
		echo.>>c:\temp\userslist.txt
		echo.>>c:\temp\userslist.txt
		echo.
		echo.
		del /f /q c:\temp\sid.txt
	)
)
echo.
echo Report is saved to c:\temp\userslist.txt
echo Missed was saved to c:\temp\USERSoffline.txt

Open in new window

0
 
SLPowersEngineerAuthor Commented:
Thank you so so very much.  You have just explaied that to me better then any one could have!

Thanks again this made my day!
0
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.

All Courses

From novice to tech pro — start learning today.