-1

I'm on my school's Cyber Patriot team and I'm trying to make some batch scripts for the windows images. I can't get my code to work the way I'm trying to get it to work, what I'm trying to do is have it run, prompt the user for input, ask for input one more time, then either create a user account or delete one. Here is my code:

@ECHO off

xterm -e start

:start
echo 0a
echo Welcome to the Master Script For Windows CyberPatriot Completion
echo ----------------------------------------------------------------
echo What would you like to do first.
echo 1...User Add /w password pre-set (meets compexity requirements)
echo 2...User Delete (all files)
pause
set /P choice1= "Please make your choice: " 
if %choice1% == 1 (
   GOTO useradd
)
if %choice1% == 2 (
   GOTO userdelete
) 

:useradd
set /P choice2= "Please enter the name of the user you want to create (the password is pre-set 
to Password123456789!): "
net user %choice2% Password123456789! /add

:userdelete
set /P choice2= "Please enter the name of the user you want to delete (WARNING! ALL FILES WILL 
BE DELETED PLEASE CHECK FIRST): "
net user %choice2% /del

Also, if you have any batch resources, language recommendations, or suggestions as to what I should/shouldn't be doing please let me know. Thanks in advance for the help :)

  • 1
    The `SET /P` command needs to be all on one line. You will need a `GOTO` after the `useradd` code so that it doesn't run the `userdelete` code. – Squashman Oct 24 '21 at 19:38
  • 1
    Are you trying to run all of this script in xterm? I'm assuming that `echo 0a`, should be `color 0a`, and you should take a look at the choice command, `choice /?` Also you should probably perform some rigorous validation of the user input before trying to use it, and I would also advise that you determine if an user name, once validated, already exists before trying to create it, and introduce an additional `Are you sure?` prompt before you `/delete` not `/del` a user too. You should also disable delayed expansion, as you are using an exclamation point in your password. – Compo Oct 24 '21 at 22:31

1 Answers1

0

There could be used the following batch script:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
color 0A

:Begin
cls
echo Welcome to the master script for Windows CyberPatriot completion
echo ----------------------------------------------------------------
echo(
echo What would you like to do first?
echo(
echo 1 ... User Add with password preset (meets complexity requirements)
echo 2 ... User Delete (all files)
echo(
%SystemRoot%\System32\choice.exe /C 12 /N /M "Please make your choice:"
if errorlevel 2 goto UserDelete
if errorlevel 1 goto UserAdd
goto Begin

:UserAdd
echo(
echo Please enter the name of the user you want to create.
echo The password is pre-set to: Password123456789!
echo(
:PromptAdd
set "NameUser="
set /P "NameUser=User name: "
if not defined NameUser goto PromptAdd
set "NameUser=%NameUser:"=%"
if not defined NameUser goto PromptAdd
echo(
%SystemRoot%\System32\net.exe user "%NameUser%" Password123456789! /ADD
echo(
if errorlevel 1 goto PromptAdd
goto EndBatch

:UserDelete
echo(
echo Please enter the name of the user you want to delete.
echo WARNING! ALL FILES WILL BE DELETED PLEASE CHECK FIRST.
echo(
:PromptDelete
set "NameUser="
set /P "NameUser=User name: "
if not defined NameUser goto PromptDelete
set "NameUser=%NameUser:"=%"
if not defined NameUser goto PromptDelete
echo(
%SystemRoot%\System32\net.exe user "%NameUser%" /DELETE
echo(
if errorlevel 1 goto PromptDelete

:EndBatch
pause
color
endlocal

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • choice /?
  • cls /?
  • color /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • net /?
  • net user /?
  • pause /?
  • set /?
  • setlocal /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143