簡體   English   中英

IF ERRORLEVEL 與 net user 命令

[英]IF ERRORLEVEL with net user command

我需要一些批次的幫助。 我使用條件處理語句創建了這個批處理文件來檢測錯誤。 對於此批處理文件,不應保留任何條件處理字符。 我需要刪除它們並使用“IF ERRORLEVEL”來檢測錯誤。 我們不能使用“IF %ERRORLEVEL%”或任何高級內容,我們只是處於介紹階段。 我們還需要使用 net user 命令。 我被困住了。

與條件處理語句一起使用的原始代碼:

@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

我知道的:

  • 如果帳戶已存在,則錯誤級別為 0。
  • 如果該帳戶不存在且已創建,則錯誤級別為 0。
  • 如果命令提示符不是作為管理員或密碼策略運行的
    滿足,錯誤級別為 2。

實際上我不知道任何這些東西,因為我的錯誤級別似乎隨着我的實驗而改變......我被卡住了......其中一個命令提示我得到了錯誤級別 1 所以......也是 GOTO : EOF條件處理?

我如何完成這個腳本? 我被困住了。 嘗試了一些東西,但最終卡住了。 這是我放棄的東西。

我沒有條件處理語句和 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

創建用戶時唯一會產生錯誤的時間是在嘗試創建用戶之后,因此錯誤檢查需要移至代碼的那部分。 但是,由於if ERRORLEVEL 0表示“如果 ERRORLEVEL 為 0 或更高”並且 2 大於 0,則您需要在net user add和最后IF ERRORLEVEL 0行之間移動IF ERRORLEVEL 2代碼塊。 此外,您希望net user失敗,因此我們需要在錯誤if errorlevel 0被拾取之前重定向腳本流。

@echo off

:: Check if the account already exists before creating account.
net user "%~1"

IF ERRORLEVEL 2 GOTO ADD_USER
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
:ADD_USER
net user %1 %1 /add

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
)

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

你的作業的措辭只提到了條件處理符號,而不是一般的所有條件處理,所以這只是意味着你不能使用|| &&

而不是嘗試使用net.exeuser命令創建用戶,然后嘗試處理這樣做的任何問題; 我將使用內置的 WMI 命令行實用程序提供以下方法。 這個想法是根據所有現有標准本地用戶的列表(不是內置/特殊的)檢查您輸入的用戶名,並且只有在重復的用戶名不存在時才繼續。

@If "%~1"=="" (Echo No username provided as an input argument.
    "%__AppDir__%timeout.exe" 3 /NoBreak>NUL&GoTo :EOF)
@SetLocal DisableDelayedExpansion
@Set "Usr=%~1"
@Call :UsrsLst "%Usr%"
@If ErrorLevel 2 (Echo The username already exists.
    "%__AppDir__%timeout.exe" 3 /NoBreak>NUL&GoTo :EOF
)Else If ErrorLevel 1 Echo You have no existing standard local users.
@If ErrorLevel 0 Echo Creating user %Usr% . . .
@"%__AppDir__%timeout.exe" 3 /NoBreak>NUL
@EndLocal&GoTo :EOF

:UsrsLst
@For /F "Delims==" %%G In ('Set Usrs 2^>NUL')Do @Set "%%G="
@For /F Tokens^=2Delims^=^" %%G In ('^""%__AppDir__%wbem\WMIC.exe"^
     Path Win32_UserProfile Where "Special!='True' And LocalPath Is Not Null"^
     Assoc:List 2^>NUL^"')Do @For /F Tokens^=4Delims^=^" %%H In (
 '^""%__AppDir__%wbem\WMIC.exe" UserAccount Where "SID='%%G'" Assoc:List^
 /ResultRole:Name 2^>NUL^"')Do @If Not Defined Usrs (Set Usrs="%%H"
    )Else Call Set "Usrs=%%Usrs%% "%%H""
@Set Usrs 2>NUL|"%__AppDir__%findstr.exe"/LI "\"%~1\"">NUL&&(Exit/B 2)||(
    If Not Defined Usrs (Exit/B 1) Else Exit/B 0)

現有的第10行僅用於演示目的,您可以僅用腳本的其余部分替換該行。 即實際創建新用戶帳戶的代碼,以及任何后續驗證過程。

除了以下建議之外,您不得修改任何其他內容。 在第4行和第5行之間,在繼續執行腳本之前,對輸入參數執行一些驗證。 這將是一種更好的方法,它嘗試在嘗試創建用戶帳戶后簡單地捕獲錯誤。

例如,以下是一些常見的 Windows 用戶名要求:

  • 提供的用戶名必須至少包含 1 個字符,但不超過 20 個字符。
  • 提供的用戶名不能只包含句點或空格。
  • 提供的用戶名不能包含以下任何字符: */\\[]: ;I=,+?<>"@

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM