Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如果使用net user命令执行ERRORLEVEL_Batch File_Cmd_Errorlevel_Wmic - Fatal编程技术网

Batch file 如果使用net user命令执行ERRORLEVEL

Batch file 如果使用net user命令执行ERRORLEVEL,batch-file,cmd,errorlevel,wmic,Batch File,Cmd,Errorlevel,Wmic,我需要一些帮助。我使用条件处理语句创建了这个批处理文件来检测错误。对于此批处理文件,不应保留任何条件处理字符。我需要删除这些并使用“IF ERRORLEVEL”来检测错误。我们不能使用“IF%ERRORLEVEL%”或任何高级功能,我们只是处于介绍阶段。我们还需要使用netuser命令。我被卡住了 使用条件处理语句的原始代码: 据我所知: 如果该帐户已存在,则错误级别为0 如果帐户不存在且已创建,则errorlevel为0 如果命令提示符不是作为管理员或密码策略运行,则不 大都会,错误级别为2

我需要一些帮助。我使用条件处理语句创建了这个批处理文件来检测错误。对于此批处理文件,不应保留任何条件处理字符。我需要删除这些并使用“IF ERRORLEVEL”来检测错误。我们不能使用“IF%ERRORLEVEL%”或任何高级功能,我们只是处于介绍阶段。我们还需要使用netuser命令。我被卡住了

使用条件处理语句的原始代码:

据我所知:

  • 如果该帐户已存在,则错误级别为0
  • 如果帐户不存在且已创建,则errorlevel为0
  • 如果命令提示符不是作为管理员或密码策略运行,则不
    大都会,错误级别为2
事实上,我对这些东西一无所知,因为我的错误水平似乎随着我的实验次数的增加而改变。。。我被卡住了。。。其中一个命令提示我得到了errorlevel 1,所以。。。GOTO:EOF条件处理也是吗?

我如何完成这个脚本?我被卡住了。尝试了一些东西,但最终还是卡住了。这是我放弃的东西

没有条件处理语句的代码,如果错误级别:


创建用户时生成错误的唯一时间是在尝试创建用户之后,因此错误检查需要转移到代码的该部分。但是,由于
if ERRORLEVEL 0
表示“if ERRORLEVEL为0或更高”,且2高于0,因此需要在
net user add
和最后一行
if ERRORLEVEL 0
之间移动
if ERRORLEVEL 2
代码块。另外,您希望
net用户
失败,因此如果选择了错误级别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.exe
用户
命令创建用户,然后在事后设法处理由此产生的任何问题;我将使用内置的WMI命令行实用程序提供以下方法。这样做的目的是对照所有现有标准本地用户的列表检查您的输入用户名(这些用户不是内置的/特殊的),并且仅当不存在重复用户名时才继续

@如果“%~1”==”(没有提供用户名作为输入参数。
“%\uuuu AppDir\uuuu%timeout.exe”3/NoBreak>NUL&GoTo:EOF)
@SetLocal DisableDelayedExpansion
@设置“Usr=%~1”
@调用:UsrsLst“%Usr%”
@如果错误级别2(回显),则用户名已存在。
“%\uuuu AppDir\uuuu%timeout.exe”3/NoBreak>NUL&GoTo:EOF
)否则,如果ErrorLevel 1 Echo您没有现有的标准本地用户。
@如果错误级别0回显正在创建用户%Usr%。
@“%\uuuu AppDir\uuuuu%timeout.exe”3/NoBreak>NUL
@EndLocal和GoTo:EOF
:UsrsLst
@对于/F“Delims=”%%G In('Set Usrs 2^>NUL')Do@Set“%%G=”
@对于/F令牌^=2Delims^=^“%%G In('^”“%\uuuuu AppDir\uuuuuu%wbem\WMIC.exe”^
路径Win32_UserProfile,其中“特殊!='True'且本地路径不为空”^
Assoc:List 2^>NUL^“')Do@For/F令牌^=4Delims^=^“%%H(
“^”“%\\\\\\\\%wbem\WMIC.exe”用户帐户,其中“SID=”%%G'”关联:列表^
/ResultRole:Name 2^>NUL^“')Do@如果未定义Usrs(设置Usrs=“%%H”
)Else调用集“Usrs=%%Usrs%%”%%H
@设置Usrs 2>NUL |“%\uu AppDir\uuu%findstr.exe”/LI“\”%1\“”>NUL&&(退出/B2)||(
如果未定义Usrs(退出/B 1),则退出/B 0)
现有行
10
仅用于演示目的,您只能用脚本的其余部分替换该行。i、 e.实际创建新用户帐户的代码,以及任何后续验证过程

除以下建议外,您不得修改任何其他内容。在
4
5
行之间,在继续脚本之前,对输入参数执行一些验证。这将是一种更好的方法,在尝试创建用户帐户后,尝试简单地捕获错误

例如,以下是一些常见的Windows用户名必备项:

  • 提供的用户名必须至少包含一个字符,但不超过二十个字符
  • 提供的用户名不能完全由句点或空格组成
  • 提供的用户名不能包含以下任何字符:
    */\[]:;I=,+?“@
注意:
如果errorlevel
实际处理为“如果errorlevel为或更高”,那么
如果errorlevel 0
只需检查errorlevel是否为负。如果不是errorlevel 1
@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
@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