net group /add /domain alternative batch file windows xp - batch-file

First post here, I'm trying to create a batch file that allows a technician to supply a text file with computer names and add them to a security group. I've written the following script that uses NET GROUP, net group /domain works, but I can't use net group %filepath% /add /domain. The environment I'm under is Windows XP. Please help! Cheers.
#echo off
title ModGroup
echo Discovering available groups on %USERDOMAIN%, please wait...
echo.
net group /domain > grouplist_.txt
set /p entgrpname=Type whole group name:
findstr /i "%entgrpname%" grouplist_.txt > tmp2_.txt
(
set /p var=
)<tmp2_.txt
del tmp2_.txt
del grouplist_.txt
set var="%var:~1%"
echo.
rem ****%var% is the group name the user specified and its been verified to exist****
msg "%username%" WARNING! - You are about to edit the %var% group! - Please take extra caution whilst using this script!
echo.
echo Please enter the full filepath of the *.txt file that contains objects that you
echo want to add/remove.
echo.
set /p filepath=Filepath:
echo.
(
set /p filepath2=
)<%filepath%
echo.
set /p addrem=Please specify if you would like to add or delete:
cls
echo You're about to %addrem% the following objects to/from %var%:
echo.
echo %filepath2%
echo.
pause
echo.
echo Processing, please wait...
echo.
for /F "tokens=1 delims=" %%i in (%filepath%) do net group %var%
%%i /%addrem% /domain
echo.
pause
exit

Related

Set /P not capturing input in IF statement

Everything in this batch script works fine, but when I enter the IF statement, for some reason set /p id= doesn't actually capture anything. In fact shortly after it will echo:
You chose session %id%.
but that will return a blank, as though nothing was entered for ID.
Any advice would be greatly appreciated.
#echo off
echo Please be sure CMD is being run as an administrator.
echo.
:loop
set /p targetpc="Which PC would you like to query for users (Hostname or IP)?: "
echo.
echo Querying %targetpc%...
echo.
quser /server:%targetpc%
echo.
set /p choice="Would you like to log a user off of %targetpc%? [Y/N]: "
echo.
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session %id%.
echo.
logoff %id% /server:%targetpc% /V
echo.
echo Done!
echo.
goto loop
)
IF /I "%choice%" EQU "N" (
goto loop
)
You are using the %id% value within the same block where it is set. This being the case, you need to use delayed expansion.
Add these lines to the top and bottom of your script, respectively:
SETLOCAL EnableDelayedExpansion
<Your script>
ENDLOCAL
Now use delayed expansion notation with your block (! around variables instead of %):
IF /I "%choice%" EQU "Y" (
echo Enter user's session ID:
set /p id=
echo.
echo You chose session !id!.
echo.
logoff !id! /server:%targetpc% /V
echo.
REM Note, replacing with a period here.
echo Done.
echo.
goto loop
)
There are tons of other questions on this site regarding delayed expansion, so a quick search within the batch-file tag should yield lots of additional info if you need it.

How do I prompt users to insert Drive letter in Batch-File file?

I have a working simple batch-file program which unhides predefined drive hidden folders.
What I want to do is:
To insert Drive letter by a user instead of predefined and other option if you suggested me.
my unhide.bat code is:
#echo off
D:
attrib -r -s -h /s /d
pause
You can use set /p DriveLetter=Please enter drive letter.. If you then run %DriveLetter% as a command, it will expand as you expect.
To make your code work:
#echo off
set /p DriveLetter=Please enter drive letter.
%DriveLetter%
attrib -r -s -h /s /d
pause
They would enter "D:" and it would accept it as such.
The only thing to note is that if someone uses an incorrect drive letter, bad stuff will happen.
Alternatively, you could use a large block with CHOICE, but that seems overkill. You'd have to define every drive letter. Thus, I won't go into detail on this.
This should do:
SET /P letter=Please give your drive letter and press ENTER:
ECHO %letter%
PAUSE
I guess that you try to unhide the folders into the USB Drive caused by a worm
If you like to detect the drive letter of your USB Key, Try this code instead,
I made this tool to unhide folders and files and delete all infected links into the USB key
#echo off
Mode con cols=98 lines=10 & Color 9E
Title Searching the Drive letter of your USB Key by Hackoo 2014
echo.
ECHO *******************************************************************************************
echo.
echo Searching the drive letter of your USB Key .......
echo.
ECHO *******************************************************************************************
wmic logicaldisk get DeviceID,DriveType /Format:CSV > %Tmp%\tmp.txt
for /f "skip=2 tokens=1-3 delims=," %%a in ('%COMSPEC% /a /c type "%Tmp%\tmp.txt"') do echo %%b %%c >> %Tmp%\tmp2.txt
for /f "tokens=1" %%i in ('%COMSPEC% /a /c type "%Tmp%\tmp2.txt" ^|Find "2"') Do (set MyUSBDrive=%%i)
Del %Tmp%\tmp.txt & Del %Tmp%\tmp2.txt
cls
echo.
ECHO *******************************************************************************************
echo.
echo The drive letter of your USB Key is %MyUSBDrive%
echo.
ECHO *******************************************************************************************
pause
cls
echo.
echo.
ECHO *******************************************************************************************
echo.
echo Press a button to delete infected shortcuts and restore hidden files in your USB key
echo.
ECHO *******************************************************************************************
pause>nul
cls
echo.
echo.
ECHO *******************************************************************************************
echo.
echo Deleting infected shortcuts and restoring hidden files
echo.
ECHO *******************************************************************************************
:: To Unhide Folders and files into your USB key
Attrib -s -h -r %MyUSBDrive%\*.* /D /S >nul 2>&1
:: To delete all infected files.lnk
Del %MyUSBDrive%\*.lnk >nul 2>&1
Explorer %MyUSBDrive%
pause

Skip Find Script in Batch if text not found

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")

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.

Prompting user to select directory in batch file

Issue:
I am attempting to have users select which folder to choose from within a specified directory and then add that folder to the address in order to execute a program within that directory.
For example, !acct! is the variable that I am using to ask the end user which account they want to access. This account is the starting or root folder for all other folders. The folder structure remains that same across all accounts EXCEPT for the second token of the folder. i.e) 123456789\*\program\setup\
The * is the folder where multiple folders exist that I would like the end user to choose from.
Once chosen, I would like the script to add that directory so that the program could be executed.
I thought by setting the variable acctDir=C:\Users\jdoe\Desktop\!acct!\ would allow me to add the acct number for the root directory, then run a FOR loop that would allow me to use a wild card in the set for /d %%i in ( !acctdir!*media1\setup ) do ( start !app! )
Any help would be greatly appreciated!
#echo off
setlocal enabledelayedexpansion
:beginning
echo.
echo.
echo ===========================================================
echo Starting on !date! !time! on !Computername!
echo ===========================================================
echo.
goto :main
:main
setlocal
set /P acct=Please type the 9 digit account number you would like to restore:
set acctDir=C:\Users\jdoe\Desktop\!acct!\
set app=setup.exe /cd
set log=c:\logs.txt
echo. Starting on !date! !time! on !Computername! >> !log!
echo.
echo The account number you selected is: !acct!
echo.
goto :user
:user
set /p answer=Is this correct (Y/N)?
echo.
if /i !answer!==y goto :yes (
) else (
echo.
echo Ok. Let's try again^^!
echo.
Pause
cls
goto :beginning
)
)
:yes
for /d %%i in ( !acctdir!*media1\setup ) do (
start !app!
)
endlocal
goto :eof
You could try something like the following:
set c=0
For /f %%a in ('dir !acctDir! /B /A D') do (
set /a c+=1
echo !c! %%a
set dir!c!=%%a
)
echo.
set /p uin="Select a directory [1-!c!]: "
set udir=!dir%uin%!
echo Selected - %udir%
This loops through a directory listing of !acctDir! and prompts the user to select a directory based on the number assigned to it.
!acctDir!\%udir% will then be the location of the directory.

Resources