dimanche 15 mars 2020

Batch - IF ERRORLEVEL with net user command

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

Aucun commentaire:

Enregistrer un commentaire