log in batch program - batch-file

i have been formulating and doing a batch program that enables the user to enter his password,just like that, and i seem to arrive at nothingness, can anybody show me how to create a batch program that enables the user to enter his password?and run a specific program when log in is successfull, by the way, i am using windows xp sp2

set /p "PASSWORD=Enter your password: "
I use the quotes so I can show the trailing space in the batch file. Otherwise it's not necessary; any trailing spaces will be echoed into the output.
set /? shows a number of ways of manipulating variables in cmd.exe

Here's something that works for me...
#echo off
:loginPASS
set /p password=Password:
if %password% == qwerty goto loginsuccessful
if %password% == %password% goto loginfailed
:loginfailed
echo Login failed, password was incorrect!
pause
cls
goto login
:loginsuccessful
echo LOGIN SUCCESSFUL!
cd <location of file (dont include its name) example: C: /Users/YOURUSERNAME/desktop>
<type in file name here>
if you want to make a username input too, then delete the loginPASS section make a new one called loginUSER and type in this:
:loginUSER
set /p username=Username
cls
if %username% == Admin goto AdminLogin
if %username% == Bob goto BobLogin
if %username% == %username% goto NoUsername
make a new section called NoUsername
:NoUsername
echo User not found, check your username again.
pause
cls
goto loginUSER
this will tell the user if they entered the wrong username.
after that make a new section for 1 of the users and name appropriately.
in this case you will name it AdminLogin.
After that repeat loginPASS and make the password for the user.
The whole code should look something like this:
#echo off
:AdminLogin
set /p password=Password:
if %password% == 158468 goto loginsuccessful
if %password% == %password% goto loginfailed
:loginfailedADMIN
echo Login failed, password was incorrect!
pause
cls
goto AdminLogin
:loginsuccessfulADMIN
echo LOGIN SUCCESSFUL!
cd <location of file (dont include its name) example: C: /Users/YOURUSERNAME/desktop>
<type in file name here>
pause
exit
You will want to make seperate login sections for each user.
Hope this helps!
P.S There might be other ways but this is what i use.

Related

The batch file keeps crashing when the user enters text

I've been trying to get this code to work...
I've tried re doing the code... to no avail also, the code below is a bit big...
:ST
if exist password.txt goto login
if not exist password.txt goto signup
:login
set /p user=<username.txt
set /p pass=<password.txt
set /p answer=Username:
if %answer% = %user% goto p1
if NOT %answer% = %user% goto errlog111
exit
:p1
set /p answer=Password:
if %answer%=%pass% goto
if NOT %answer% = %pass% goto errlog111
echo success
pause
exit
:signup
title Username needed for account creation! / Newex / Unregistered
set /p answer=Username:
echo %answer% > username.txt
cls
set /p answer=Password:
echo %answer% > password.txt
cls
echo Account created!
goto login
exit
:errorlog111
echo Username / Password incorrect!
pause
goto login
So, what I want is for it to check the contents of password.txt, (or username.txt) for what the user inputs, and if the user inputs the right thing, it goes on to the next thing. However, if they get the password, or username wrong then I want it to say "Username / Password wrong!" but for some reason when I enter the username, the batch file just closes! Please help!
if %answer% = %user% goto p1
The correct syntax is to use == or EQU.
Better syntax is
if "%answer%" == "%user%" goto p1
the quotes protect against some illegal inputs and where the arguments contain certain characters like , ; and would be advised for any comparison between strings.
Note that if /i ... makes the comparison case-insensitive (for future reference)
Also - a hard-to-see error
echo %answer% > username.txt
will include the space which follows the value of answer into the file
> username.txt echo %answer%
will not include this (presumably) unwanted space.

Batch file errors

I am working on a batch file
#echo off
color 0a
:start
cls
echo "create" (create password)
echo "check" (check password)
echo.
set /p PROGRAM= What do you want to do?:
goto %PROGRAM%
and I want to make it so if you don't enter "create" or "check" it gives you an invalid message and sends you back to the start of the program
(not looking for "if not defined". I am looking for if you type something other then "create" or "check")
#echo off
color 0a
:start
cls
echo "create" (create password)
echo "check" (check password)
echo.
set /p PROGRAM= What do you want to do?:
if "%PROGRAM%" == "create" goto create
if "%PROGRAM%" == "check" goto check
echo error message here
pause
goto start
i think thats pretty basic, not much explaination needed, but if you have questions, feel free to ask.

Setting User account "Full Name" and "Description" from within a batch using variables

I am attempting to write a batch script that adds new users to a machine using user prompts to get the information. I am at the point where the user gets created correctly, but the "Full Name" and "Description" fields don't populate, no matter what I try. I can't seem to get it to pass the %fullname% variable into either the net user command or the wmic command. If I just type quoted text into either spot it adds the information properly. Also if there's a better way to do echos like that it would be appreciated, takes up too many lines IMO. :)
:User
echo.
echo Please Input Username...
echo.
set /p username=Username:
echo.
echo Please Confirm Username...
echo.
set /p confirm=Username:
if '%username%' == '%confirm%' (
echo.
echo Please Input User Full Name...
echo.
set /p fullname=Full Name:
echo.
echo Please Input User Description...
echo.
set /p description=Description:
echo Creating %username%...
net user %username% * /add /active:yes /fullname:%fullname% /comment:%description% /passwordchg:no /expires:never
wmic useraccount where "Name='%username%'" set PasswordExpires=FALSE
wmic useraccount where "Name='%username%'" set FullName=%fullname%
This script creates the user just fine, but the fullname and comment fields are blank.
Problem solved. Apparently the parentheses for my if statement were messing everything up. The fullname and description variables were blank for whatever reason. I guess you can't do a set inside an if statement?
No matter, changing to this has solved the problem and it works perfectly now.
:User
echo.
echo Please Input Username...
echo.
set /p username=Username:
echo.
echo Please Confirm Username...
echo.
set /p confirm=Username:
if '%username%' == '%confirm%' goto User1
goto User
:User1
echo.
echo Please Input User Full Name...
echo.
set /p fullname=Full Name:
echo.
echo Please Input User Description...
echo.
set /p description=Description:
pause
echo Creating %username%...
net user %username% * /add /active:yes /fullname:"%fullname%" /comment:"%description%" /passwordchg:no /expires:never
wmic useraccount where "Name='%username%'" set PasswordExpires=FALSE

RunAs Prompt for credentials

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

Need Assistance in creating a password/username system with batch

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. :)

Resources