远程解锁用户

时间:2014-05-21 20:23:50

标签: batch-file windows-xp

我正在尝试创建一个远程解锁Windows帐户的批处理文件。基本上我要做的是为Windows登录提供批处理文件提示,这样做后,检查AD以验证用户是否存在,如果是,继续解锁,如果没有恢复到开头提示进行Windows登录。它几乎可行,我只是无法弄清楚脚本的IFIF NOT部分,即使IF NOT是有效的用户登录,似乎uname也会运行。

我的批处理脚本看起来像这样

@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  

2 个答案:

答案 0 :(得分:1)

试试这个:

@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

我们正在使用findstr将输出过滤为用户名而我们正在使用for /f来读取{{{}的输出1}}并将其设置为名为NET user /Domain %uname% ^| findstr %uname%的变量。然后我们使用uname_check if语句查看变量是否已定义(只有在用户存在时才会定义)

答案 1 :(得分:0)

我的实验如下:

@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

我认为我们可以使用ERRORLEVEL 0来描述用户是否存在。

OOT:如何静默输出NET USER命令..? :)

谢谢。

相关问题