If statement Else命令在批处理中同时运行?

If statement Else命令在批处理中同时运行?,if-statement,batch-file,wmic,If Statement,Batch File,Wmic,我不知道为什么会发生这种情况,但我正在处理的批处理脚本中的else命令似乎是同时运行的,而不是一次运行一行。有什么建议吗 set getprocesslistlocal=wmic process get name,processid,workingsetsize echo Type the name of the remote machine to view processes of (or type local for local machine), and press Enter. set

我不知道为什么会发生这种情况,但我正在处理的批处理脚本中的else命令似乎是同时运行的,而不是一次运行一行。有什么建议吗

set getprocesslistlocal=wmic process get name,processid,workingsetsize
echo Type the name of the remote machine to view processes of (or type local for local machine), and press Enter.
set /P remotemachine=
if %remotemachine%==local (
%getprocesslistlocal%
) else (
echo Type the user name to access %remotemachine% with, then press Enter.
set /P remoteuser=
echo Type the password for %remoteuser% on %remotemachine%, then press Enter. ^(Will be displayed in plaintext^)
set /P remotepassword=
wmic /node:%remotemachine% /user:%remoteuser% /password:%remotepass% process get name,processid
)
echo End of list.
pause
echo Type the process id to terminate and hit Enter.
set /P killid=
if %remotemachine%==local (
wmic process where processid="%killid%" call terminate
) else (
wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process where processid="%killid%" call terminate
)
echo Process id %killid% terminated.
pause

它认为问题在于密码变量的简单输入错误。输入时。使用的变量名称是
remotepassword
,在
wmic
命令中使用时,使用的名称是
remotepass
,因此它将始终为空:

set /P remotepassword=
rem    ^^^^^^^^^^^^^^
wmic /node:%remotemachine% /user:%remoteuser% /password:%remotepass% process get name,processid
rem                                                      ^^^^^^^^^^

它认为问题在于密码变量的简单输入错误。输入时。使用的变量名称是
remotepassword
,在
wmic
命令中使用时,使用的名称是
remotepass
,因此它将始终为空:

set /P remotepassword=
rem    ^^^^^^^^^^^^^^
wmic /node:%remotemachine% /user:%remoteuser% /password:%remotepass% process get name,processid
rem                                                      ^^^^^^^^^^

中,如果
用于
代码块,则需要延迟扩展和
!变数用于所有值不断变化的变量。例如:

setlocal enabledelayedexpansion
...
) else (
echo Type the user name to access %remotemachine% with, then press Enter.
set /P remoteuser=
echo Type the password for !remoteuser! on %remotemachine%, then press Enter. ^(Will be displayed in plaintext^)
set /P remotepassword=
wmic /node:%remotemachine% /user:!remoteuser! /password:!remotepass! process get name,processid
)

中,如果
用于
代码块,则需要延迟扩展和
!变数用于所有值不断变化的变量。例如:

setlocal enabledelayedexpansion
...
) else (
echo Type the user name to access %remotemachine% with, then press Enter.
set /P remoteuser=
echo Type the password for !remoteuser! on %remotemachine%, then press Enter. ^(Will be displayed in plaintext^)
set /P remotepassword=
wmic /node:%remotemachine% /user:!remoteuser! /password:!remotepass! process get name,processid
)

我不确定您所说的“我正在处理的批处理脚本中的else命令似乎是一次性运行的,而不是一次运行一行。”

但是你的第一个ELSE块确实有问题

当解析语句时,使用
%var%
进行正常扩展,同时解析整个IF/ELSE构造。因此,您的ELSE块正在查看在执行设置值的SET/P语句之前存在的
%remoteuser%
%remotepass%
的值。显然不是你想要的

这个问题很容易通过使用延迟扩展来解决。延迟扩展必须首先使用SETLOCAL EnableDelayedExpansion启用,然后在执行时使用
扩展变量!瓦尔而不是
%var%

正如MichaelBurr在回答中指出的,您还必须与变量名保持一致

我还没有测试过,但我相信以下方法会奏效:

@echo off
setlocal enableDelayedExpansion
set getprocesslistlocal=wmic process get name,processid,workingsetsize
echo Type the name of the remote machine to view processes of (or type local for local machine), and press Enter.
set /P remotemachine=
if %remotemachine%==local (
  %getprocesslistlocal%
) else (
  echo Type the user name to access %remotemachine% with, then press Enter.
  set /P remoteuser=
  echo Type the password for !remoteuser! on %remotemachine%, then press Enter. ^(Will be displayed in plaintext^)
  set /P remotepass=
  wmic /node:%remotemachine% /user:!remoteuser! /password:!remotepass! process get name,processid
)
echo End of list.
pause
echo Type the process id to terminate and hit Enter.
set /P killid=
if %remotemachine%==local (
  wmic process where processid="%killid%" call terminate
) else (
  wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process where processid="%killid%" call terminate
)
echo Process id %killid% terminated.
pause

有关延迟扩展的详细信息,请在命令提示符下键入
HELP SET
SET/?
,然后开始阅读以“最后,已添加对延迟环境变量扩展的支持”开头的段落,大约读到一半。我不清楚您的意思“我正在处理的批处理脚本中的else命令似乎是同时运行的,而不是一次运行一行。”

但是你的第一个ELSE块确实有问题

解析语句时,使用
%var%
进行正常扩展,同时解析整个IF/ELSE构造。因此,ELSE块将看到在执行设置值的SET/p语句之前存在的
%remoteuser%
%remotepass%
的值。显然不是您想要的

使用延迟扩展很容易解决此问题。延迟扩展必须首先使用SETLOCAL EnableDelayedExpansion启用,然后在执行时使用
!var!
而不是
%var%
来扩展变量

正如MichaelBurr在回答中指出的,您还必须与变量名保持一致

我还没有测试过,但我相信以下方法会奏效:

@echo off
setlocal enableDelayedExpansion
set getprocesslistlocal=wmic process get name,processid,workingsetsize
echo Type the name of the remote machine to view processes of (or type local for local machine), and press Enter.
set /P remotemachine=
if %remotemachine%==local (
  %getprocesslistlocal%
) else (
  echo Type the user name to access %remotemachine% with, then press Enter.
  set /P remoteuser=
  echo Type the password for !remoteuser! on %remotemachine%, then press Enter. ^(Will be displayed in plaintext^)
  set /P remotepass=
  wmic /node:%remotemachine% /user:!remoteuser! /password:!remotepass! process get name,processid
)
echo End of list.
pause
echo Type the process id to terminate and hit Enter.
set /P killid=
if %remotemachine%==local (
  wmic process where processid="%killid%" call terminate
) else (
  wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process where processid="%killid%" call terminate
)
echo Process id %killid% terminated.
pause

有关延迟扩展的详细信息,请在命令提示符下键入
HELP SET
SET/?
,然后开始阅读大约一半的段落,该段落以“最后,已添加对延迟环境变量扩展的支持”开头。“

我想您是指批处理文件中的第一个?else命令同时运行到底是什么意思?您的意思是它从不停止,让您为两个
set/P
命令输入任何内容?您应该更清楚地描述发生了什么(以及您预期会发生什么)。我收到了“无效用户ID”,我假设它来自正在输入的其他命令。一旦我输入了
remoteuser
,它就不会出现在下一行中。该用户确实存在,并且在批处理结束时,该命令与该用户和密码配合良好。我想您是指批处理文件中的第一个其他用户吧?else命令同时运行到底是什么意思?您的意思是它从不停止,让您为两个
set/P
命令输入任何内容?您应该更清楚地描述发生了什么(以及您预期会发生什么)。我收到了“无效用户ID”,我假设它来自正在输入的其他命令。一旦我输入了
remoteuser
,它就不会出现在下一行中。此用户确实存在,并且在批外时,该命令与此用户和密码配合良好。感谢您非常详细的回复。你的代码确实有效,我一定会从现在开始(需要时)使用延迟扩展。谢谢你非常详细的回复。您的代码确实可以工作,从现在起(需要时),我肯定会使用延迟扩展。这是一个问题,但不是唯一的问题。谢谢你的贡献。这是个问题,但不是唯一的问题。谢谢你的贡献。