Skip Find Script in Batch if text not found - batch-file

I have a script that should look through a text file, for its workstation ID and then assign the user next to its Workstation ID to be a local admin.
However, if the computer name is not found, I would like it to simply goto the end of the file, and exit as if it were completed successfully.
I have looked around, but I can't really think of what terms I would use to search for in google.
Any help is much appreciated.
#echo off
setlocal enabledelayedexpansion
FIND /i "%computername%" < "C:\Build\Elevate\AccessElevations.txt" >%temp%\computer.txt
FOR /F "tokens=1-6 delims=," %%a in (%temp%\computer.txt) DO (
set school=%%a
set computerid=%%b
set Model=%%c
set Serial=%%d
set user=%%e
set access=%%f
If /i "%%f" EQU "Admin" goto Elevate
If /i "%%f" EQU "NoAdmin" goto End
:Elevate
set desc=!school! - !computerid! - !model! - !serial! - !user!
echo.
echo Now creating local admin account for !user! on !computerid!
echo.
echo Description: !desc!
echo.
net localgroup Administrators "GBN\!user!" /add
net config server /srvcomment:"!desc!"
)
:End

After the FIND /i ... line
if errorlevel 1 goto :eof
which goes to the special label :eof which CMD understands as EndOfFile if the errorlevel established by find is non-zero (zero means "found it")

Related

REG Query to find a specify value of registry and if it exist than go to

I want to find a specify value of registry and if it exist than go to execute the job
Here my script used but not successful
:SkinReplacementKIS64
CLS
ECHO.
REG Query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\KasperskyLab\AVP19.0.0\settings" /v "EnableSelfProtection" |find "1" >nul & IF %ErrorLevel% EQU 0 (
ECHO.
ECHO Please disable Kaspersky self-defense first before doing the action!
ECHO.
pause
GOTO SkinReplacementKIS64
) else (
CLS
ECHO ***************************************************************************************
ECHO Kaspersky Tweaker v1.4 for KFA, KAV, KIS, KTS, KSC (19.0.0.1088)
ECHO ***************************************************************************************
ECHO.
net stop AVP19.0.0
dark_skin_kis.exe /p12345678
net start AVP19.0.0
ECHO.
pause
GOTO KIS64
)
what am I wrong ? Please help
Use for /fto get the value an test it?
#echo off
:SkinReplacementKIS64
CLS
ECHO.
for /f "delims=" %%a in ('REG Query "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\KasperskyLab\AVP19.0.0\settings" /v "EnableSelfProtection"') do set "$val=%%a"
if %$val%==1 (
ECHO.
ECHO Please disable Kaspersky self-defense first before doing the action!
ECHO.
pause
GOTO SkinReplacementKIS64
) else (
CLS
ECHO ***************************************************************************************
ECHO Kaspersky Tweaker v1.4 for KFA, KAV, KIS, KTS, KSC (19.0.0.1088)
ECHO ***************************************************************************************
ECHO.
net stop AVP19.0.0
dark_skin_kis.exe /p12345678
net start AVP19.0.0
ECHO.
pause
GOTO KIS64
)
Based on my comment, the easiest way would be to exclude the first line from your potential match:
Reg Query "HKLM\SOFTWARE\WOW6432Node\KasperskyLab\AVP19.0.0\settings" /V "EnableSelfProtection" 2>Nul | Find /V "HKLM" | Find "1" >Nul ...
You may even wish to use x1 instead of 1 to provide more certainty too.

Batch File Skipping over FOR LOOP

UPDATE
I removed the tokens=4 and it started outputting data. It is not skipping past the FOR LOOP. I was skipping too far ahead with the tokens. I am still a little confused as to why it works as a single batch and not from this batch but now at least I know what the issue was. Thank you to everyone that was looking into this for me.
I am writing a script to copy over data from one computer to another. The issue is that it is skipping over the FOR LOOP that I am calling from another FOR LOOP. If you are testing the script it requires two PC's and a mapped T: drive to somewhere on the second computer. I can write the script so it looks for an external drive if that is more helpful to someone.
FOR /F "tokens=4 skip=1" %%a in ('REG QUERY "%_regshell%" /v "%_regdesktop%"') DO (
SET _dt=%%a
echo robocopy "!_dt!" "!_NetworkDrive!\!_fndesktop!" !_params!
echo attrib -h -r "!_NetworkDrive!\!_fndesktop!"
)
If I write the FOR LOOP above in a batch by itself and just echo out %%a then it works without a problem. In this, I can see that it is indeed calling :_backup but it skips directly over the FOR Loop and I am not sure why. I have written scripts like this many times but never had any that just completely ignore the FOR Loop. Can anyone take a look and assist? Thank you.
#echo off
:: Set Variables
SET _driveID=T:
SET _params=/Z /E /COPY:DT /R:1 /W:0 /XD LocalService NetworkService temp "temporary internet files" winsxs Content.IE5 cache /XF ntuser.* *.tmp /XJ /FP /NC /NS /NP /NJH
SET _regshell=HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
SET _regdesktop=Desktop
:: Set Current Directory
pushd %SystemDrive%\
:: Start Menu - Create Choices and Options. Send to various places to perform the actions.
:_start
cls
ECHO Please type either option 2 or 3 and then press ENTER on the keyboard?
Echo 2. TRANSFER FILES FROM DESKTOP TO LAPTOP
Echo 3. EXIT THE PROGRAM
echo.
set /p choice=Enter Number:
if '%choice%'=='2' goto _desktopToLaptop
if '%choice%'=='3' goto :EOF
echo "%choice%" is not a valid option. Please try again
echo.
goto _start
:: Detect Drive Letters
:_desktopToLaptop
setlocal EnableDelayedExpansion
FOR /F "usebackq skip=1" %%a IN (`WMIC logicaldisk where DeviceID^="%_driveID%" get caption`) DO (
SET _NetworkDrive=%%a
if exist %%a (
CALL :_backup
goto :EOF
) else (
echo.
echo The laptop does not appear to be attached to the computer.
echo.
pause
goto :EOF
)
)
:_backup
:: Detect the folder locations and begin to backup each location to the laptop.
FOR /F "tokens=4 skip=1" %%a in ('REG QUERY "%_regshell%" /v "%_regdesktop%"') DO (
SET _dt=%%a
echo robocopy "!_dt!" "!_NetworkDrive!\!_fndesktop!" !_params!
echo attrib -h -r "!_NetworkDrive!\!_fndesktop!"
)
echo we are past the for loop
pause
:: Return to directory program was run from
popd
If anyone else runs into this issue or something similar, check your tokens and your skip. Mine worked just fine as a single batch but when I included as a call I had to change the options from tokens=4 skip=1 to tokens=3* skip=2 in order to get the correct output.
The correct tokens in that FOR LOOPS should be:
#echo off
:: Set Variables
SET _driveID=T:
SET _params=/Z /E /COPY:DT /R:1 /W:0 /XD LocalService NetworkService temp "temporary internet files" winsxs Content.IE5 cache /XF ntuser.* *.tmp /XJ /FP /NC /NS /NP /NJH
SET _regshell=HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
SET _regdesktop=Desktop
:: Set Current Directory
pushd %SystemDrive%\
:: Start Menu - Create Choices and Options. Send to various places to perform the actions.
:_start
cls
ECHO Please type either option 2 or 3 and then press ENTER on the keyboard?
Echo 2. TRANSFER FILES FROM DESKTOP TO LAPTOP
Echo 3. EXIT THE PROGRAM
echo.
set /p choice=Enter Number:
if '%choice%'=='2' goto _desktopToLaptop
if '%choice%'=='3' goto :EOF
echo "%choice%" is not a valid option. Please try again
echo.
goto _start
:: Detect Drive Letters
:_desktopToLaptop
setlocal EnableDelayedExpansion
FOR /F "usebackq skip=1" %%a IN (`WMIC logicaldisk where DeviceID^="%_driveID%" get caption`) DO (
SET _NetworkDrive=%%a
if exist %%a (
CALL :_backup
goto :EOF
) else (
echo.
echo The laptop does not appear to be attached to the computer.
echo.
pause
goto :EOF
)
)
:_backup
:: Detect the folder locations and begin to backup each location to the laptop.
FOR /F "tokens=3* skip=2" %%a in ('REG QUERY "%_regshell%" /v "%_regdesktop%"') DO (
SET _dt=%%a
echo robocopy "!_dt!" "!_NetworkDrive!\!_fndesktop!" !_params!
echo attrib -h -r "!_NetworkDrive!\!_fndesktop!"
)
echo we are past the for loop
pause
:: Return to directory program was run from
popd
Given that the main issue in your script appears to be the setting of a variable to the data within the defined registry key and value, you could use:
Set "_regshell=HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders"
Set "_regdesktop=Desktop"
Set "_dt="
For /F "EOL=H Tokens=2*" %%A In ('Reg Query "%_regshell%" /V "%_regdesktop%"'
) Do Set "_dt=%%~B"
If Not Defined _dt GoTo :EOF
Echo "%_dt%"

Batch file over network path

I have the following batch script to make life easier at work.
Here is what it is supposed to work:
1- Drag and drop some file onto the .bat
2- Choose file's destination on the "menu"
3- Script copy's files to destination folder
4- Script executes remote procedure (that's the PSexec line)
5- Script copy's the result of the remote procedure to other folders.
And this works fine... except for a "small" detail with which i need some help.
When i try to copy the network location \10.250.39.116\d%... if i haven't previously logged into that machine it wont work.
I've been looking into the 'net use' command to overcome this, but i'm not sure if it suits my needs.
There are a total of 4 different machines i need to authenticate, dependent on the choice of the menu.
Actual Question:
Can i log in to such machines with the batch, and avoid creating duplicate connections every time i run the script ? If so, how?
Thank you for your time!
:)
I know the paths I have all point to the same place :)
#echo off
setlocal enabledelayedexpansion
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Compilar em Qualidade
ECHO 2 - Compilar na HSDEV
ECHO 3 - Compilar nas DEMOS
ECHO 4 - EXIT
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO :QUAL
IF %M%==2 GOTO :HSDEV
IF %M%==3 GOTO :DEMO
IF %M%==4 GOTO EOF
:QUAL
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:HSDEV
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:DEMO
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:PROCESSA
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~nx"
set "pathVec[!argCount!]=%%~dpx"
)
rem echo Number of processed arguments: %argCount%
for /L %%i in (1,1,%argCount%) do (
echo Vou compilar %%i - "!argVec[%%i]!"
if exist %PathForms6%!argVec[%%i]!.* del /q %PathForms6%!argVec[%%i]!.*
if exist %PathForms10%!argVec[%%i]!.* del /q %PathForms10%!argVec[%%i]!.*
robocopy "!pathVec[%%i]!." %PathForms6% !argVec[%%i]!.fmb > nul
)
c:
cd c:\pstools
psexec %PathCompilador%
for /L %%i in (1,1,%argCount%) do (
if exist "%PathForms10%!argVec[%%i]!.fmx" (
xcopy %PathForms10%!argVec[%%i]!.fmx %PathDestinoPriv% /y
xcopy %PathForms10%!argVec[%%i]!.fmx %PathDestinoPub% /y)
)
pause
How much testing have you done with net use? Try running it twice at the command line. Notice how the output changes at the second running:
As you can see, where a connection has already been established, net use will output a summary of the connection rather than creating a duplicate connection.
If you prefer, you could use conditional execution or errorlevel checking. Using this method, you can avoid calling net use until xcopy fails, which should only be the first time. Here's a short example, simply to illustrate the mechanics:
#echo off
setlocal
ping -n 1 10.250.39.116 | find /i "TTL=" >NUL || (
echo 10.250.39.116 is offline. Unable to continue. Press any key to exit.
pause >NUL
goto :EOF
)
call :xcopy "%~1" "destination"
echo Press any key to exit.
pause >NUL
net use \\10.250.39.116\d$ /delete >NUL 2>NUL
goto :EOF
:xcopy <source> <dest_dir>
xcopy /L "%~1" "%~2" 2>NUL || (
net use \\10.250.39.116\d$ /user:username password >NUL 2>NUL
xcopy /L "%~1" "%~2"
)
goto :EOF

Batch File if command; is there any way to complete a certain task if a variable includes a certain character?

Ok, I have this batch file, and I would like to detect if the user's input includes a certain character, like ..
Here is what I have so far...
IF /I %option%== ATTRIB +h
(I would like it to detect if there is a . pressed)
I would like the .bat to detect If there is something other than Attrib +h pressed, like a ., for a file extension. So that if there was a file given, I would not ask for a file name again, because it wouldn't run properly!
Here is a part of the code, for Magoo.
#echo off
title iControl
color 80
:home
cls
echo iControl: Logan Murphy Version 1.5.7
echo Copyright 2014 Logan Murphy. All rights reserved.
GOTO jiki
:jiki
title iHelp
echo.
set /p "SSL=%USERNAME%>Execute>"
echo %SSL%"|find "." >nul
cls
if errorlevel 1 (echo notfound) else (ATTRIB +h)
rem IMPLEMENTED IT HERE!
rem Here we go
rem need to fix execution
IF /I %SSL%==attrib goto attrib
IF /I %SSL%==pause goto pause
IF /I %SSL%==backup goto backup
Edit: Ok, the answers from magoo and john really helped, but if i wanted this to detect a folder name, as well as a file name, what would i change?
Edit: Ok Magoo, testing it now.
Edit: That doesnt really work well, as I am trying to find out if the variable INCLUDES a certain character, not if the whole variable is = to a valid file/folder name, but nice try. Will mess around with it a bit... Thanks for your time Magoo, I appreciate it:)
Edit: I think I worked it out! Thanks everyone who helped me!
Here is my new code...
#echo off
title iControl
color 80
:home
cls
echo iControl: Logan Murphy Version 1.5.7
echo Copyright 2014 Logan Murphy. All rights reserved.
GOTO jiki
:jiki
title iHelp
echo.
set /p "SSL=%USERNAME%>Execute>"
%SSL%
cls
goto Seconds
:Seconds
IF /I %SSL%==attrib goto attrib
IF /I %SSL%==attrib +h goto attrib+-
IF /I %SSL%==attrib -h goto attrib+-
IF /I %SSL%==attrib +r goto attrib+-
IF /I %SSL%==attrib -r goto attrib+-
The general formula would be
echo "%var%"|find "characterorstringrequired" >nul
if errorlevel 1 (echo notfound) else (echo found)
So, for your instance,
echo "%option%"|find "i" >nul
if errorlevel 1 (echo notfound) else (ATTRIB +h)
Beyond which, I have to be as vague as your question, I'm afraid...
After seeing part-code...
echo "%SSL%"|find "." >nul
cls
if errorlevel 1 (echo notfound) else (ATTRIB +h)
I'm not sure whether cls affects errorlevel. The test should be executed directly after the command that establishes errorlevel - which is the echo %SSL... line.
echo "%SSL%"|find "." >nul
if errorlevel 1 (echo notfound) else (ATTRIB +h)
Serious damage can be done by cmd if the commands are not understood. The attrib command requires a target, so it should be
... else (attrib +h something)
to set something to hidden.
beyond that - it's not a good idea to use cmd keywords like attrib, pause and backup as labels. It can cause damage in the case of a tyop.
The benefit of sying what you want to do instead of how you want to do whatever-it-is.
To test for a file's existence, try
if exist "%SSL%" (echo "%SSL%" exists) else (echo "%SSL%" is not there)
where echo can be any legitimate (series of) command(s).
What about something like this for all special chars ?
#echo off
:_Start
set /p "FID="
set FID="%FID:"=%"
setlocal EnableDelayedExpansion
for %%I in (^| ^& ^< ^> ^^ ^( ^) ' ` ^%% ^") do set FID=!FID:%%I=!
setlocal DisableDelayedExpansion
set FID="%FID:!=%"
endlocal&set FID=%FID:~1,-1%
:_Back
set _Flag1=
for /f "tokens=1* delims=~=*;,?-+\/.##${}[]: " %%J in ('echo !FID!') do (
set FID=%%J%%K
set _Flag1=%%J
set _Flag2=%%K
)
if NOT "%_Flag2%"=="" goto :_Back
if NOT defined _Flag1 goto :_Start
endlocal&set FID=%FID%
echo %FID%
pause
The VBA command for finding a character in a string is INSTR so if you are looking for a . for example then in VBA could could use INSTR. There are two ways to do what you want:
Write a QBASIC or VBscript version of INSTR and then call that from a BATCH script
Write a batch script that does INSTR.
If you are interested in the second one look at http://www.dostips.com/forum/viewtopic.php?f=3&t=1986
It isn't as efficied as the first one would be because INSTR is pretty much a one liner in VBScript and the batch version does a character by character comparison, but if you are looking for a batch only solution that may do what you want.

Loop not looping in batch file

I have an issue and can't seem to see where I am going wrong.
I have this code, that is meant to loop through a text file, and look at the last word of the line to see if the user at the start of the line should receive elevated access to their assigned laptop.
However the code just runs through once, and then exits.
#echo off
cls
echo.
echo The following script will process the entire list of students, giving each
echo student administration rights to the laptop and set the laptop description
echo where necessary.
echo.
pause
cls
FOR /F "tokens=1-6 delims=," %%a IN (accesselevations.txt) DO (
set School1=%%a
set LaptopID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
If /i "%%f" EQU "Admin" goto Elevate2
If /i "%%f" EQU "NoAdmin" goto NoElevate2
:Elevate2
set Desc1=!School1! - !LaptopID! - !Model1! - !Serial1! - !User1!
echo.
echo Now creating local admin account for !user! on !LaptopID!
echo.
echo Description: !Desc1!
echo.
pause
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net localgroup Administrators "GBN\!User1!" /add
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net config server /srvcomment:"!Desc1!"
:NoElevate2
cls
Echo.
Echo User !user1! is not allowed Local Administrator rights on !LaptopID!.
pause
)
pause
:End
The file AccessElevations has confidential data in it so I am not able to post it unfortunately, however it contains something like this
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
School,WorkstationID,LaptopModel,LaptopSerial,StudentUsername,AdminOrNot
...and so-on.
I hope I have given enough information, I'm fairly new here.
Thanks in advance for any light you can spread on the issue.
Toby
Use setlocal enabledelayedexpansion or you won't able to use exclamation point on variables.
Close your FOR loop before Elevate2
Change this:
If /i "%%f" EQU "Admin" goto Elevate2
If /i "%%f" EQU "NoAdmin" goto NoElevate2
To:
If /i "%%f" EQU "Admin" (CALL:Elevate2) else (CALL:NoElevate2)
Using CALL command, your script will return to the end of FOR loop after elevation operation is done, GOTO command connot do this.
Put exit/b before :NoElevate2, otherwise your script won'r return to CALL command point.
Your code should be like this:
#echo off
setlocal enabledelayedexpansion
cls
echo.
echo The following script will process the entire list of students, giving each
echo student administration rights to the laptop and set the laptop description
echo where necessary.
echo.
pause
cls
FOR /F "tokens=1-6 delims=," %%a IN (accesselevations.txt) DO (
set School1=%%a
set LaptopID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
If /i "%%f" EQU "Admin" (CALL:Elevate2) else (CALL:NoElevate2))
:Elevate2
set Desc1=!School1! - !LaptopID! - !Model1! - !Serial1! - !User1!
echo.
echo Now creating local admin account for !user! on !LaptopID!
echo.
echo Description: !Desc1!
echo.
pause
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net localgroup Administrators "GBN\!User1!" /add
psexec \\!LaptopID! -u GBN\!ocusr! -p !ocpw! -n 10 -e net config server /srvcomment:"!Desc1!"
exit/b
:NoElevate2
cls
Echo.
Echo User !user1! is not allowed Local Administrator rights on !LaptopID!.
pause
exit/b
:End
#echo off
setlocal
cls
echo.
echo The following script will process the entire list of students, giving each
echo student administration rights to the laptop and set the laptop description
echo where necessary.
echo.
pause
cls
FOR /F "tokens=1-6 delims=," %%a IN (accesselevations.txt) DO (
set School1=%%a
set LaptopID=%%b
set Model1=%%c
set Serial1=%%d
set User1=%%e
set Access1=%%f
If /i "%%f" EQU "Admin" CALL :Elevate2
If /i "%%f" EQU "NoAdmin" CALL :NoElevate2
)
GOTO :EOF
:Elevate2
set Desc1=%School1% - %LaptopID% - %Model1% - %Serial1% - %User1%
echo.
echo Now creating local admin account for %user1% on %LaptopID%
echo.
echo Description: %Desc1%
echo.
ECHO psexec \\%LaptopID% -u GBN\%ocusr% -p %ocpw% -n 10 -e net localgroup Administrators "GBN\%User1%" /add
ECHO psexec \\%LaptopID% -u GBN\%ocusr% -p %ocpw% -n 10 -e net config server /srvcomment:"%Desc1%"
GOTO :eof
:NoElevate2
Echo.
Echo User %user1% is not allowed Local Administrator rights on %LaptopID%.
GOTO :EOF
Notes:
setlocal added to ensure environment does not get polluted by a run
Doesn't appear to be much point in setting Access1 since it isn't used.
CALL :routine rather than goto - Not a ood idea to goto within a block as some cmd versions appear to work differently. Colon is required for running internal subroutine
Within internal subroutines, can use %var% since these run in their own context.
!var! syntax is only effective when invoked by setlocal enabledelayedexpansion (not executed in original batch.)
psexec commands simply ECHOed. Need to remove the preceding echo to execute the psexec
Shouldn't a 'remove privileges' routine be applied for 'Nodamin' ?
!user! changed to %user1% in :elevate2 since user1 is established by the loop, not user.
Suitable datafile used for testing was:
School1,Workstation1ID,Laptop1Model,Laptop1Serial,Student1Username,Admin
School2,Workstation2ID,Laptop2Model,Laptop2Serial,Student2Username,NoAdmin
School3,Workstation3ID,Laptop3Model,Laptop3Serial,Student3Username,Admin
School4,Workstation4ID,Laptop4Model,Laptop4Serial,Student4Username,NoAdmin
not that hard to construct - could easily be done with a prettier version by substituting for names and other sensitive information.

Resources