我需要人帮我弄些东西。我使用条件处理语句创建了这个批处理文件来检测错误。对于此批处理文件,不应保留任何条件处理字符。我需要删除这些,并使用“如果ERRORLEVEL”来检测错误。我们不能使用“如果%ERRORLEVEL%”或任何高级,我们只是在介绍阶段。我们还需要使用need命令。我被卡住了。
用于条件处理语句的原始代码:
@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据我所知:
,实际上,我不知道这些东西,因为我的错误程度似乎随着我实验的越多而改变.我卡住了..。其中一个命令提示我的错误级别1,所以.也是GOTO :EOF条件处理吗?
我如何完成这个脚本?我被卡住了。尝试了一些事情但最终还是失败了。这是我放弃的东西。
没有条件处理语句的代码,如果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发布于 2020-03-15 12:23:27
在创建用户时生成错误的唯一时间是在尝试创建用户之后,因此错误检查需要移到代码的这一部分。但是,由于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作业的措辞只提到了条件处理符号,而不是所有的条件处理,所以这只是意味着您不能使用||或&&。
发布于 2020-03-16 12:16:39
之后,我不再尝试使用net.exe的user命令创建用户,然后尝试处理由此产生的任何问题;我将使用内置的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用户名要求:
*/\[]: ;I=,+?<>"@。https://stackoverflow.com/questions/60691130
复制相似问题