有没有一种方法可以使输入循环直到dsquery成功?

时间:2019-04-02 23:26:03

标签: batch-file command-line active-directory

我正在尝试为批处理文件找到一种方法,以便在dsget不成功的情况下重复一个问题。

这是我目前拥有的:

@echo off
set /p input="Enter AD Username to lookup: "
dsquery user -samid %input% |dsget user -memberof |dsget group -samid |find /v "samid"|findstr /v "dsget%" | clip | echo User found & echo.Groups copied to clipboard
pause

它将用户的活动目录组复制到剪贴板,但是如果不成功,则必须重新打开批处理文件,我不想发生这种情况。

基本上,如果dsquery无法成功找到用户,我希望它说类似

User not found
Enter an AD Username to lookup:

,并且只有成功,它才会裁剪dsquery的内容

1 个答案:

答案 0 :(得分:0)

我没有dsquery或dsget,所以无法测试。

请参见one-line conditional execution in windows1one-line conditional execution in windows2

command1 && command2 || command3

仅当command1成功时才执行command2。如果command1失败,请执行command3。

if %errorlevel% neq 0

@echo off
:loop 
set /p input="Enter AD Username to lookup: "
REM Since I can't test it this is a 51/49 guess
REM MAYBE?
dsquery user -samid %input% | dsget user -memberof | dsget group -samid  | FIND /i "dsget failed" > NUL && goto :loop || clip
REM dsquery user -samid %input% | dsget user -memberof | dsget group -samid | FINDSTR /i /c:"dsget failed">NUL | clip
REM IF %ERRORLEVEL% NEQ 0 goto :loop
echo User found. Groups copied to clipboard.
pause 
exit