vendredi 26 mars 2021

Batch File - IF statement issue related to the net user command

Hello to all my fellow StackOverflow members! I am trying to setup a somewhat basic script to help with checking user account details on a domain controller in a domain oriented Corp environment. One of the IT Service managers setup a very watered down script that basically just runs the following command:

net user /domain

I have tried to build a better script since we use this in so many of our customer environments it cuts down on guess work and time overall. The problem is at the end of the script I setup a direct back to the top of the script with a y/n prompt.. however, if you fail to press Y or N (case insensitive) and rather press j or 3 for example the script terminates. I have been extremely unsuccessful in setting up a basic error handling routine that states if the reported value is neither y nor n, echo a simple error string and then repeat the prompt. The following is my code for the setup (I have removed the previously attempts at setting up the looped routine for the y/n values. I have changed the values of the actual paths to each GOTO section so as not to share customer oriented information.

@ECHO OFF

:Start
SET uname=
cls

IF "%userdomain%"=="D1" GOTO 1
IF "%userdomain%"=="D2" GOTO 2
IF "%userdomain%"=="D3" GOTO 3
IF "%userdomain%"=="D4" GOTO 4

:1
ECHO You are on the D1 domain (%userdomain%)
ECHO Usernames should be in the format of firstname.lastname
ECHO.
SET /P uname=Username: 
IF "%uname%"=="" GOTO Error
net user %uname% /domain
GOTO Request

:2
ECHO You are on the D2 Domain (%userdomain%)
ECHO Username format varies based on the creation of the ID.
ECHO.
SET /P uname=Username: 
IF "%uname%"=="" GOTO Error
net user %uname% /domain
GOTO Request

:3
ECHO You are on the D3 Domain (%userdomain%)
ECHO Usernames are formatted as First Initial Last Name (auser)
ECHO.
SET /P uname=Username: 
IF "%uname%"=="" GOTO Error
net user %uname% /domain
GOTO Request

:4
ECHO You are on the D4 Domain (%userdomain%)
ECHO Usernames are formatted as First Initial Last Name (auser)
ECHO.
SET /P uname=Username: 
IF "%uname%"=="" GOTO Error
net user %uname% /domain
GOTO Request

:Error
ECHO.
ECHO Username was left blank, please enter a valid username and try again
ECHO.
TIMEOUT /T 5
GOTO Start

:Request
ECHO.
SET /P resp=Do you want to perform another lookup Y/N?
IF /I "%resp%"=="y" GOTO Start
IF /I "%resp%"=="n" exit

In the :Request section above I tried using IF /I NOT and IF NOT /I to begin the statement, as well as using a true false oriented value setting a variable to true, then making the statement say if not y if not n set variable to false, then using if false, repeat the loop, so far hitting any key other than y or n just exits the prompt.

Any help is appreciated, and thanks in advance!

Aucun commentaire:

Enregistrer un commentaire