我正在使用批处理脚本来执行基本的用户功能(添加用户,删除,更改帐户类型等),而不是手动完成所有操作。程序按照A,B,C,...,G的选项询问用户他们想做什么。这部分工作正常,但是,在用户输入选项后,它马上关闭。检查后,if语句之后的命令将不运行。我应该如何修复if语句?
@echo off
net user
echo What would you like to do?
echo A: Turn off default admin account
echo B: Turn off default guest account
echo C: Add a user
echo D: Remove a user
echo E: Change a users privilages
echo F: Change a password
echo G: Nothing
set /p en = "Enter in the Letter:"
if %en% == "A" (
net user administrator /active:no
echo Built-In Admin Is Turned Off
Pause
)
if %en% == "B" (
net user guest /active:no
echo Guest Account Is Turned Off
Pause
)
if %en% == "C" (
set /p adduser = What is the name of the user you would like to add(Case Sensitive)?
set /p addpassword = What is the password you would like to add to %adduser%?
net user %adduser% %addpassword% /added
echo Added %adduser%
set /p objUser = GetObject(WinNT://MyComputer/%adduser%,user)
%objUser%.PasswordExpired = 1
%objUser%.SetInfo
echo Password Will Need To Be Changed On Next Logon
Pause
if %en% == "D" (
set /p deluser = What is the name of the user you would like to delete(Case Sensitive)?
net user %deluser% /del
echo User %deluser% has been Deleted
该代码应该能够运行与字母相对应的命令,然后保持打开状态,直到用户准备关闭它为止。但是,它将不会运行该序列,并在用户输入字母以设置变量“ en”时关闭。
如果不是给您带来不便,请解释为什么您的代码以更简单的方式运行。我对使用批处理脚本还很陌生,但仍然不了解绝大多数命令和术语。
答案 0 :(得分:1)
最终代码应如下所示:
@echo off
net user
echo What would you like to do?
echo A: Turn off default admin account
echo B: Turn off default guest account
echo C: Add a user
echo D: Remove a user
echo E: Change a users privilages
echo F: Change a password
echo G: Nothing
set /p en="Enter in the Letter:"
choice /c:ABCDEFG /M "Enter in the letter:" /N
if errorlevel 7 (
rem This means the letter is 'G'; add some code here:
)
if errorlevel 6 (
rem This means the letter is 'F'; add some code here:
)
if errorlevel 5 (
rem This means the letter is 'E'; add some code here:
)
if errorlevel 4 (
set /p "deluser=What is the name of the user you would like to delete (Case Sensitive)? "
net user %deluser% /del
echo User %deluser% has been Deleted
pause
)
if errorlevel 3 (
set /p "adduser=What is the name of the user you would like to add (Case Sensitive)? "
set /p "addpassword=What is the password you would like to add to %adduser%? "
net user %adduser% %addpassword% /added
echo Added %adduser%
set /p "objUser=GetObject(WinNT://MyComputer/%adduser%,user) "
%objUser%.PasswordExpired = 1
%objUser%.SetInfo
echo Password Will Need To Be Changed On Next Logon
pause
)
if errorlevel 2 (
net user guest /active:no
echo Guest Account Is Turned Off
Pause
)
if errorlevel 1 (
net user administrator /active:no
echo Built-In Admin Is Turned Off
Pause
)
所有固定的问题都在评论中提到。键入if /?
,set /?
和choice /?
,以获取有关cmd的更多信息。