Batch file 在问两个问题时,有没有更好的方法来完成这个选择命令序列?
我的问题是关于命令Batch file 在问两个问题时,有没有更好的方法来完成这个选择命令序列?,batch-file,Batch File,我的问题是关于命令选项的问题。我将一个问题嵌套在另一个问题中,因为我需要问一个问题,然后如果是将用户添加到组中,然后问下一个问题添加另一个用户。但是如果第一个问题的回答是否,我只想问另一个问题添加另一个用户 @echo off setlocal enabledelayedexpansion openfiles > NUL 2>&1 if NOT %ERRORLEVEL% EQU 0 goto NotAdmin GOTO create :NotAdmin echo This c
选项的问题。我将一个问题嵌套在另一个问题中,因为我需要问一个问题,然后如果是将用户添加到组中,然后问下一个问题添加另一个用户
。但是如果第一个问题的回答是否,我只想问另一个问题添加另一个用户
@echo off
setlocal enabledelayedexpansion
openfiles > NUL 2>&1
if NOT %ERRORLEVEL% EQU 0 goto NotAdmin
GOTO create
:NotAdmin
echo This command prompt is NOT ELEVATED
GOTO END
:create
set /p userName="Please enter a new user name to create:"
net user !userName! > NUL 2>&1
if %ERRORLEVEL% EQU 0 GOTO ERROR
set /p Passwd="Please enter a password:"
set /p FullName="Please enter user's full name:"
net user !userName! !Passwd! /ADD /FullNAME:"!FullName!" > NUL 2>&1
if %ERRORLEVEL% EQU 0 (
echo command completed succuessfully
) else (
echo command did not compelte
)
choice /C yn /M "Would you like to add the user to the local Administrators group"
if ERRORLEVEL 2 (
choice /C yn /M "Would you like to create another user "
if ERRORLEVEL 2 (
GOTO END
)
if ERRORLEVEL 1 (
GOTO create
)
)
if ERRORLEVEL 1 (
net localgroup Administrators !userName! /ADD
)
:ERROR
echo The user !userName! already exist
pause
GOTO create
:END
只是提取选择部分。这里我使用了两种方法。一个是创建标签ch1
和ch2
,其中choice命令被告知转到ch%errorlevel%
,在这种情况下,errorlevel将是1
或2
第二个has使用通常的if errorlevel 1
,但没有第二个条件,因为如果errorlevel 1
的第一个条件不满足,它将直接进入goto end
choice /C yn /M "Would you like to add the user to the local Administrators group"
goto ch%errorlevel%
:ch1
echo add the user to the group here
:ch2
choice /C yn /M "Would you like to create another user "
if errorlevel 1 cls & goto create
goto end
只是提取选择部分。这里我使用了两种方法。一个是创建标签ch1
和ch2
,其中choice命令被告知转到ch%errorlevel%
,在这种情况下,errorlevel将是1
或2
第二个has使用通常的if errorlevel 1
,但没有第二个条件,因为如果errorlevel 1
的第一个条件不满足,它将直接进入goto end
choice /C yn /M "Would you like to add the user to the local Administrators group"
goto ch%errorlevel%
:ch1
echo add the user to the group here
:ch2
choice /C yn /M "Would you like to create another user "
if errorlevel 1 cls & goto create
goto end
我建议此任务使用以下批处理文件代码:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
%SystemRoot%\System32\openfiles.exe >NUL 2>&1
if errorlevel 1 goto NotAdmin
goto Create
:NotAdmin
echo This command prompt is NOT ELEVATED.
goto END
:Create
set "NameUser="
set /P "NameUser=Please enter a new user name to create: "
if not defined NameUser goto Create
set "NameUser=!NameUser:"=!"
if not defined NameUser goto Create
%SystemRoot%\System32\net.exe user "!NameUser!" >NUL 2>&1
if not errorlevel 1 (
echo The user !NameUser! exists already.
pause
goto Create
)
set /P "Passwd=Please enter a password: "
set /P "FullName=Please enter user's full name: "
%SystemRoot%\System32\net.exe user "!NameUser!" "!Passwd!" /ADD /FullNAME:"!FullName!" >NUL 2>&1
if errorlevel 1 (
echo Creation of user account failed.
) else (
echo Creation of user account completed succuessfully.
)
%SystemRoot%\System32\choice.exe /C NY /N /M "Would you like to add the user to the local administrators group? [Y/N]"
if errorlevel 2 %SystemRoot%\System32\net.exe localgroup Administrators "!NameUser!" /ADD
%SystemRoot%\System32\choice.exe /C NY /N /M "Would you like to create another user? [Y/N]"
if errorlevel 2 goto Create
:END
endlocal
环境变量USERNAME
是一个预定义的Windows环境变量,可以在打开并运行set user
时看到,该变量列出了所有环境变量,其值以字符串user
开头,不区分大小写。另请参阅维基百科中关于的文章。因此,批处理文件中使用变量NameUser
,而不是userName
在命令提示符窗口中运行IF/?
时,有关命令IF输出的帮助说明了用于评估ERRORLEVEL
的建议语法,该语法保留了以前执行的程序的退出代码。上的答案在示例中详细解释了此语法。在所有操作系统上,强烈建议不允许任何可执行文件以负值退出。此处使用并由Microsoft for Windows编写的可执行文件永远不会以负值退出
上述批处理代码中的所有可执行文件都以完全限定的文件名引用,以使批处理文件独立于本地环境变量的当前值PATH
和PATHEXT
。因此Windows命令处理器不能搜索要执行的文件
通过交换选择列表中的字母Y
和N
,优化非常简单。在命令提示窗口中运行choice/?
时的帮助输出说明,如果用户按下选项列表中第一个字符的键,则choice
将以1
退出;如果用户按下选项列表中第二个字符的键,则以2
退出。现在,用户按Y键时,刚创建的用户帐户将添加到本地管理员组,然后提示用户再创建一个帐户,如按N键
使用命令choice
的第二个问题也通过交换的Y
和N
指定,以使在单个IF条件下,在用户按下键Y时跳转到标签下的代码Create
,否则在恢复后继续退出批处理文件处理明确先前的本地环境
要了解所使用的命令及其工作方式,请打开一个窗口,在其中执行以下命令,并非常仔细地阅读为每个命令显示的所有帮助页面
choice/?
echo/?
endlocal/?
goto/?
如果/?
net/?
net localgroup/?
net用户/?
openfiles/?
暂停/?
设置/?
setlocal/?
我建议此任务使用以下批处理文件代码:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
%SystemRoot%\System32\openfiles.exe >NUL 2>&1
if errorlevel 1 goto NotAdmin
goto Create
:NotAdmin
echo This command prompt is NOT ELEVATED.
goto END
:Create
set "NameUser="
set /P "NameUser=Please enter a new user name to create: "
if not defined NameUser goto Create
set "NameUser=!NameUser:"=!"
if not defined NameUser goto Create
%SystemRoot%\System32\net.exe user "!NameUser!" >NUL 2>&1
if not errorlevel 1 (
echo The user !NameUser! exists already.
pause
goto Create
)
set /P "Passwd=Please enter a password: "
set /P "FullName=Please enter user's full name: "
%SystemRoot%\System32\net.exe user "!NameUser!" "!Passwd!" /ADD /FullNAME:"!FullName!" >NUL 2>&1
if errorlevel 1 (
echo Creation of user account failed.
) else (
echo Creation of user account completed succuessfully.
)
%SystemRoot%\System32\choice.exe /C NY /N /M "Would you like to add the user to the local administrators group? [Y/N]"
if errorlevel 2 %SystemRoot%\System32\net.exe localgroup Administrators "!NameUser!" /ADD
%SystemRoot%\System32\choice.exe /C NY /N /M "Would you like to create another user? [Y/N]"
if errorlevel 2 goto Create
:END
endlocal
环境变量USERNAME
是一个预定义的Windows环境变量,可以在打开并运行set user
时看到,该变量列出了所有环境变量,其值以字符串user
开头,不区分大小写。另请参阅维基百科中关于的文章。因此,批处理文件中使用变量NameUser
,而不是userName
在命令提示符窗口中运行IF/?
时,有关命令IF输出的帮助说明了用于评估ERRORLEVEL
的建议语法,该语法保留了以前执行的程序的退出代码。上的答案在示例中详细解释了此语法。在所有操作系统上,强烈建议不允许任何可执行文件以负值退出。此处使用并由Microsoft for Windows编写的可执行文件永远不会以负值退出
上述批处理代码中的所有可执行文件都以完全限定的文件名引用,以使批处理文件独立于本地环境变量的当前值PATH
和PATHEXT
。因此Windows命令处理器不能搜索要执行的文件
通过交换选择列表中的字母Y
和N
,优化非常简单。在命令提示窗口中运行choice/?
时的帮助输出说明,如果用户按下选项列表中第一个字符的键,则choice
将以1
退出;如果用户按下选项列表中第二个字符的键,则以2
退出。现在,用户按下Y键,就可以创建我们了