if errorlevel
帮助
if
说:
IF [NOT] ERRORLEVEL number command
作为
如果错误级别
条件。那就是,你
必须
if errorlevel n
计算结果为
如果退出代码是
至少
所以呢
if errorlevel 1 ...
捕获任何错误(通过退出代码发出信号),而
if errorlevel 0 ...
不管怎样,你可能想要一个
if not errorlevel 1 ...
跳过行
for /f
命令有参数
skip=n
可用于在开始时跳过行。如果您的输出以两行不需要的行开始,那么您可以这样做
for /f "skip=2 tokens=1" %%Q in ('query termserver') do
迭代中的多个已知值
用于/f
第二个代码片段的问题是
for
迭代
. 因此,当您给它一个环境变量时,它将对它进行标记化(并将标记放入不同的变量中),但循环只运行
每行一次
set
这里有一点容易出错,因为你可能会得到比你想要的更多。像这样的
for /f ... in ("%TermServers%") ...
那就容易多了。不过,这并不能解决最初的问题。解决这一问题的最简单方法可能如下:
rem space-separated list of servers
set TermServers=Server1 Server2 Server3 Server7 Server8 Server10
rem call the subroutine with the list of servers
call :query_servers %TermServers%
rem exit the batch file here, to prevent the subroutine from running again afterwards
goto :eof
rem Subroutine to iterate over the list of servers
:query_servers
rem Process the next server in the list
rem Note the usage of %1 here instead of a for loop variable
echo Checking %1
for /f "tokens=1" %%U in ('query user %UserID% /server:%1') do (echo %%Q)
rem Remove the first argument we just processed
shift
rem if there is still another server to be processed, then do so
rem we're mis-using the subroutine label as a jump target here too
if not [%1]==[] goto query_servers
rem This is kind of a "return" statement for subroutines
goto :eof
预计到达时间:
我又一次错过了最明显的答案:
set TermServers=Server1 Server2 Server3 Server7 Server8 Server10
for %%S in (%TermServers%) do (
for /f "tokens=1" %%U in ('query user %UserID% /server:%1') do (echo %%Q)
)
对于
,