My batch script does the following:
user types in username and is added to a variable
trick to ask for password (hides input from user) and adds to variable
checks username and password authentication for domain GROUP using "net user" command
If user is found in set group, continue to map drive.
If user is not part of group restart at beginning
This script works when the username is found.
This script works when a username is found in a group
The problem is if the username is NOT FOUND.
When the user is NOT FOUND, it reports the "More help is available by typing NET HELPMSG 2221."
It just sits there and does not continue to prompt or anything.
I echoed the errorlevel and it comes out to 0, and reports the 0. However it still just sits there never reaching command prompt or going where the GOTO tells it.
I have put in errorlevels and I am unsure why it is stuck after the error message and does not continue. It is as if the batch script is not releasing from somewhere.
#echo off
:Question
Echo.
Echo.
SET /P HelperName=Enter Witness' Name:
Echo.
If %HelperName% EQU %Username% GOTO SameUserName
cls
echo hP1X500P[PZBBBfh#b##fXf-V#`$fPf]f3/f1/5++u5>%temp%\ftp.com
set /p password=What is your password? <nul
for /f "tokens=*" %%i in ('%temp%\ftp.com') do set "password=%%i"
del %temp%\ftp.com
cls
set i=0
set group=WGD
set user=%HelperName%
echo Checking if %user% is member of %group%...
for /f %%f in ('"net user %user% /domain | findstr /i %group%"') do set /a i=%i%+1
if %i% gtr 0 (goto :member)
:nomember
echo %user% is not member of %group% Please Try Again
goto :question
:member
net use L: \\10.10.10.10\foldernamehere\TEMP /user:wgd\%helpername% %password%
if [%errorlevel%]==[0] goto deletedrive
goto error
:deletedrive
net use /delete L:
goto start
It is as if the script in still in another function.
Well it seems like I was not waiting long enough for the message to finish. It hangs for a bit, then continues on through the script. The wait however for the help message to clear is a little long, if you know how to clear faster please let me know.
Related
I need help with some batch. I created this batch file using conditional processing statements to detect errors. For this batch file, NO conditional processing characters should remain. I need to remove those and use "IF ERRORLEVEL" to detect errors. We cannot use "IF %ERRORLEVEL%" or anything advanced, we are just in an introductory phase. We need to use the net user command as well. I am stuck.
Original Code that Works with Conditional Processing Statements:
#echo off
rem Check if the account already exists before creating account. If it does not exist, go to :FAIL
net user | find /i "%1" >nul 2>&1 && GOTO FAIL
rem Add a user if the account does not exist and then go to :SUCCESS
net user %1 %1 /add >nul 2>&1 || GOTO ERROR
GOTO SUCCESS
rem If the user does not already exist and is succesfully added
:SUCCESS
echo The %1 user was succesfully added.
GOTO :EOF
rem If the user already exists
:FAIL
echo The %1 user already exists on this computer.
echo Please use a different username.
GOTO :EOF
rem Send system generated errors messages to ECHO
:ERROR
echo An Error was generated when attempting to create the user
echo These are the things you can check:
echo Did you open the command prompt as administrator?
echo If passwords are required on your system, did you include one?
GOTO :EOF
:END
What I know:
If the account already exists the errorlevel is 0.
If the account does not exist and it is created, the errorlevel is 0.
If the command prompt is not run as an admin or password policy not
met, the errorlevel is 2.
Actually I don't know any of that stuff because it seems like my errorlevel is changing the more I experiment... I'm stuck... One of the command prompts I was getting errorlevel 1 so... also is GOTO :EOF conditional processing?
How do I accomplish this script? I am stuck. Tried some things but ultimately stuck. Here's what I have that I gave up on.
Code I have with no conditional processing statements and IF ERRORLEVEL:
#echo off
IF ERRORLEVEL 2 (
rem Send system generated errors messages to ECHO
echo An Error was generated when attempting to create the user
echo These are the things you can check:
echo Did you open the command prompt as administrator?
echo If passwords are required on your system, did you include one?
GOTO :EOF
)
:: Check if the account already exists before creating account. If it does not exist, go to :FAIL
net user | find /i "%1"
IF ERRORLEVEL 0 (
rem If the user already exists
echo The %1 user already exists on this computer.
echo Please use a different username.
GOTO :EOF
)
:: Add a user if the account does not exist and then go to :SUCCESS
net user %1 %1 /add
IF ERRORLEVEL 0 (
REM If the user does not already exist and is succesfully added
echo The %1 user was succesfully added.
GOTO :EOF
)
:END
The only time an error would be generated when creating a user would be right after trying to create a user, so that error check needs to move to that part of the code. However, since if ERRORLEVEL 0 means "if ERRORLEVEL is 0 or higher" and 2 is higher than 0, you need to move the IF ERRORLEVEL 2 code block between the net user add and last IF ERRORLEVEL 0 lines. Also, you want the net user to fail, so we need to redirect script flow before if errorlevel 0 gets picked up.
#echo off
:: Check if the account already exists before creating account.
net user "%~1"
IF ERRORLEVEL 2 GOTO ADD_USER
IF ERRORLEVEL 0 (
rem If the user already exists
echo The %1 user already exists on this computer.
echo Please use a different username.
GOTO :EOF
)
:: Add a user if the account does not exist and then go to :SUCCESS
:ADD_USER
net user %1 %1 /add
IF ERRORLEVEL 2 (
rem Send system generated errors messages to ECHO
echo An Error was generated when attempting to create the user
echo These are the things you can check:
echo Did you open the command prompt as administrator?
echo If passwords are required on your system, did you include one?
GOTO :EOF
)
IF ERRORLEVEL 0 (
REM If the user does not already exist and is succesfully added
echo The %1 user was succesfully added.
GOTO :EOF
)
:END
The wording of your homework only mentions conditional processing symbols, not all conditional processing in general, so that just means you can't use || or &&.
Instead of trying to create a user using net.exe's user command, and then try to handle any issues from doing so, afterwards; I would offer the following approach using the built-in WMI command line utility wmic. The idea is to check your input username against a list of all existing standard local users, (which aren't built-in/special), and only proceed if a duplicate username doesn't already exist.
#If "%~1"=="" (Echo No username provided as an input argument.
"%__AppDir__%timeout.exe" 3 /NoBreak>NUL&GoTo :EOF)
#SetLocal DisableDelayedExpansion
#Set "Usr=%~1"
#Call :UsrsLst "%Usr%"
#If ErrorLevel 2 (Echo The username already exists.
"%__AppDir__%timeout.exe" 3 /NoBreak>NUL&GoTo :EOF
)Else If ErrorLevel 1 Echo You have no existing standard local users.
#If ErrorLevel 0 Echo Creating user %Usr% . . .
#"%__AppDir__%timeout.exe" 3 /NoBreak>NUL
#EndLocal&GoTo :EOF
:UsrsLst
#For /F "Delims==" %%G In ('Set Usrs 2^>NUL')Do #Set "%%G="
#For /F Tokens^=2Delims^=^" %%G In ('^""%__AppDir__%wbem\WMIC.exe"^
Path Win32_UserProfile Where "Special!='True' And LocalPath Is Not Null"^
Assoc:List 2^>NUL^"')Do #For /F Tokens^=4Delims^=^" %%H In (
'^""%__AppDir__%wbem\WMIC.exe" UserAccount Where "SID='%%G'" Assoc:List^
/ResultRole:Name 2^>NUL^"')Do #If Not Defined Usrs (Set Usrs="%%H"
)Else Call Set "Usrs=%%Usrs%% "%%H""
#Set Usrs 2>NUL|"%__AppDir__%findstr.exe"/LI "\"%~1\"">NUL&&(Exit/B 2)||(
If Not Defined Usrs (Exit/B 1) Else Exit/B 0)
The existing line 10 is included just for demonstration purposes, you would replace that line only with the rest of your script. i.e. The code to actually create your new user account, and any subsequent verification processes.
You must not modify anything else except for the following recommendation. Between lines 4 and 5, perform some validation of the input argument, before continuing with the script. This would be a much better approach that trying to simply capture an error after trying to create your user account.
For example, here are some common Windows username requisites:
The provided username must consist of at least one character but no more than twenty characters.
The provided username must not consist exclusively of periods or whitespace.
The provided username cannot contain any of the following characters: */\[]: ;I=,+?<>"#
.
Hi i wanted to change the value of hostname for new computer but things didn't work as i try to input the value and getting the value for the hostname. I try to restart the computer the result is still the same.. I am testing to see whether this script is capable of updating the hostname for pcs or computer name
thanks
here is my code
REM This script runs in MS-DOS Batch File Language
#echo off
set /p id= Enter ID or Hostname:
echo %id%
WMIC computersystem where caption='%ComputerName%' rename %id%
REM exit the applications
echo "Export completed successfully. Press any key to exit"
pause >nul
exit /B
Here's how I'd probably do it:
#Echo Off
Echo Your current name is %ComputerName%
:AskID
Set "ID="
Set /P "ID=Enter your new name: "
If Not Defined ID (Echo Can not be empty
GoTo AskID)
If /I "%ID%"=="%ComputerName%" Exit /B
If "%ID:~,1%"=="." (Echo Must not begin with a period
GoTo AskID)
Rem Put here some more checks for disallowed words or characters
WMIC ComputerSystem Where Name="%ComputerName%" Call Rename "%ID%"
Notes
This will need to be run 'As administrator'.
The change will not take effect until the next reboot.
It is important that you don't allow your end user, especially when running an administrative task to just enter anything at the prompt. Please consider following the Remarked line's advice.
I tested the below batch script to change the windows 10 hostname through batch script and it work perfectly fine try it out and reboot the system atlast !
set /p NEW_NAME="Please enter computer name: "
for /f %%i in ('hostname') do set OLD_NAME=%%i
echo %OLD_NAME%
echo %NEW_NAME%
WMIC computersystem where caption="%OLD_NAME%" rename "%NEW_NAME%"
pause
So I am experimenting and trying to get better at using batch but I'm stuck on an issue, and I cant figure it out. The save/load allows the user to input a password and save it to the blah.blah, then later load it using the password. The problem is I want the program to recognize if the user has a password and if they dont, it will give them the option to exit or make a new password.
:passwordwant
cls
set /p "passwant=Enter your new password: "
(
echo %passwant%
)>save.sav
goto :passwordload
pause >nul
:passwordload
cls
echo Enter password to enter the programme
set /p "pass=>"
if %pass%== %passwant% goto :home
<save.sav(
set /p pass=
)
You can use if exist to determine if a file exists (or if not exist to determine if it doesn't).
if not exist save.sav (
echo Password not found
REM If you're using Windows XP or earlier, you need to use set /p instead of choice
choice /c:NQ /M "Press N to create a password. Press Q to quit."
if %errorlevel% equ 0 goto :passwordwant
if %errorlevel% equ 1 exit /b
)
I'm trying to write a batch script that will do a RunAs for any given windows service (using explorer.exe to test) for any given user that can be input at time of running. What I currently have is this.
set /p Var1="Domain = "
set /p Var2="Username = "
set /p Var3="Service to open= "
RunAs /user:%Var1%\%Var2% "%Var3%" /seperate
On command line, this seems to work ok (with coded values instead of variables) but in a batch file it just seems to repeat itself, without opening what it's been specified (it doesn't even prompt for a password, so I can only presume its not even trying). Any idea why it's looping and what I can do to stop it?
Cheers
Hi #Gary Barnett i created something similar to this awhile back.
Here is an example;
#ECHO OFF
set /P Domainn=Enter Domain Name:
set /P Usern=Enter Username:
set userunas=runas /user:%domainn%\%usern% "
:optionmenu
CLS
ECHO 1 - Control.exe
ECHO 2 - MSinfo32.exe
ECHO 3 - Explorer.exe
ECHO q - Quit
ECHO.
set /P optionnum=Enter command number:
GOTO option%optionnum%
:option1
%userunas%control.exe
goto optionmenu
:option2
%userunas% %commonprogramfiles%\micros~1\MSInfo\msinfo32.exe"
goto optionmenu
:option3
%userunas%explorer.exe
goto optionmenu
:optionq
EXIT
Note: You must run the batch file as Administrator (right-click -> Run as Administrator) or it wont work.
/seperate is not a switch from RUNAS. So,
RUNAS /USER:"%Var1%\%Var2%" "%Var3%"
should go fine. If /seperate is a parameter to %Var3% program, use:
RUNAS /USER:"%Var1%\%Var2%" "%Var3% /seperate"
You may also add PAUSE at end of script so you can check results, while "debugging".
Okay so heres what I'm trying to do. I'm trying to make a batch file with a user input for a login. I would like to have the ability to create a new account. I have an idea, I just don't know how to go around implementing it. I have the user input his desired username and password and have it save to a text document like so:
#echo off
set /p user=Enter your desired username:
set /p pass=Enter your desired password:
echo %user% >> log.txt
echo %pass% >> log.txt
That works fine for me, but now what I would like to do is call up those two lines so that when they entered the correct username and password it will take them to their menu.
I know for a fact the the call won't work well with this. Is there any way to do what I'm trying to do?
P.S. I am aware that the txt file is not secure. I have ways around that.
You may do this to recover the saved values:
(
set /P savedUser=
set /P savedPass=
) < log.txt
This way:
if "%user%" == "%savedUser%" if "%pass%" == "%savedPass%" goto accessGranted
Another way to save the values is this:
echo set savedUser=%user%> log.bat
echo set savedPass=%pass%>> log.bat
and to recover the saved values:
call log
I'm decently proud of this :) This is pretty much a complete overhaul of your method.
This has two different files: login.bat which handles the login and login2.bat which handles the registration.
login.bat:
#echo off
choice /c:RL /m "Choose an option: Register (R) or Login (L).:
if errorlevel 2 goto login
if errorlevel 1 goto register
:register
start /wait C:\[path]\login2.bat
cls
goto login
:in
cls
echo Welcome %u%
echo.
echo Bla Bla Bla or start "a program"
pause
exit
:login
set /p u=Username
set /p p=Password
and login2.bat:
#echo off
:a
set /p a="Choose a Username"
set /p b="Choose a Password"
echo.
choice /m "Are you sure you would like your Username to be %a% and your Password to be %b%?"
if errorlevel 2 goto a
set q="if %%u%% equ %a% if %%p%% equ %b% goto in"
for /f "tokens=*" %%I in (%q%) do set m=%%I
echo %m% >>C:\[path]\login.bat
exit
The reason why :in, the label which handles what happens after you login successfully has to be in the middle instead of at the end of login.bat is so that the username/password combinations could be appended to the :login label. Obviously you could add other embellishments and change the name of the files, but this is my basic design.
Hope this helps!
This is what I do
#echo off
:: some code here
set /p username=please create a username:
set /p password=please create a password:
echo %username%>username.txt
echo %password%>password.txt
This will create a text file for both your username and password
I hope this helped. :)