user3662423
user3662423

Reputation: 21

Remotely Unlock User

I am trying to create a batch file that will unlock windows accounts remotely. Basically what I'm looking to do is have the batch file prompt for the Windows Login, after doing so, check it AD to verify if user exist or not, if so continue to unlock, and if not revert back to the beginning prompting for the Windows Login. It pretty much works I just cant figure out the IF and IF NOT parts of the script, seems like IF NOT runs even though the uname is a valid User Login.

My batch script looks like this

@echo off
:Set
SET /P uname=Please enter User Login:
NET user /Domain %uname% 
If EXIST %uname% GOTO Unlock
If NOT EXIST %uname% GOTO Set
:Unlock
Net user /Domain /Active:YES %uname%

pause  

Upvotes: 2

Views: 2595

Answers (2)

Rhak Kahr
Rhak Kahr

Reputation: 770

I do my experiment as below:

@echo off
setlocal
net user %1 /domain>nul>nul
if not %errorlevel% equ 0 (
echo %errorlevel%: User is not exist.
) else (
echo %errorlevel%: User is exist.
)
rem 0 = The user name is found.
rem 2 = The user name could not be found.
rem  or System error 1722 has occurred.
rem     The RPC server is unavailable.
rem  or System error 1355 has occurred.
rem     The specified domain either does not exist or could not be contacted.
endlocal

I think we can use ERRORLEVEL 0 to describe that the user is exist.

OOT: How to silent output of NET USER command..? :)

Thank you.

Upvotes: 0

Alex
Alex

Reputation: 917

try this:

@echo off
:PROMPT_USERNAME
SET /P uname=Please enter User Login:
for /f "tokens=3 delims= " %%u in ('NET user /Domain %uname% ^| findstr %uname%') do set uname_check=%%u

if defined uname_check (
    GOTO Unlock
) else (
    GOTO PROMPT_USERNAME
)

:Unlock
Net user /Domain /Active:YES %uname%

pause

What's happening is that we're using the findstr to filter the output to just the username and we're using for /f to read the output of NET user /Domain %uname% ^| findstr %uname% and setting that to a variable named uname_check. Then we're seeing if the variable is defined (it will only be defined if the user exists) using the if else statements

Upvotes: 1

Related Questions