Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 请求用户输入时批处理脚本中出现语法错误(/P)_Windows_Batch File_Syntax Error_Wmic - Fatal编程技术网

Windows 请求用户输入时批处理脚本中出现语法错误(/P)

Windows 请求用户输入时批处理脚本中出现语法错误(/P),windows,batch-file,syntax-error,wmic,Windows,Batch File,Syntax Error,Wmic,我问过几个人问题是什么,两次都没有解决。我以前很少玩batch,所以这可能是一个简单的错误。该代码目前应该给出使用wmic的进程列表。最后,我想将其设置为终止进程(我应该能够相当容易地完成),但我必须首先克服这个障碍 @echo off set getprocesslistlocal=wmic process get name,processid set /P remotemachinecheck=Type the name of the remote machine to view proce

我问过几个人问题是什么,两次都没有解决。我以前很少玩batch,所以这可能是一个简单的错误。该代码目前应该给出使用wmic的进程列表。最后,我想将其设置为终止进程(我应该能够相当容易地完成),但我必须首先克服这个障碍

@echo off
set getprocesslistlocal=wmic process get name,processid
set /P remotemachinecheck=Type the name of the remote machine to view processes of (or type local for local machine), and press Enter.
if %remotemachinecheck%==local
(
%getprocesslistlocal%
) else (
set remotemachine=%remotemachinecheck%
set /P remoteuser=Type the user name to access %remotemachine% with, then press Enter.
set /P remotepassword=[Type the password for %remoteuser% on %remotemachine%, then press Enter. Watch your back, it won't be hidden!
set getprocesslistremote=wmic /node %remotemachine% /user:%remoteuser% /password:%remotepass% process get name,processid
%getprocesslistremote%
)
echo End of list. Press any key to choose process to kill.
@echo off
pause>null

您要做的第一件事是注释掉
@echo off
(或将其更改为
@echo on
),这样您就可以准确地看到是哪一行导致了错误


第二件事你应该注意的是,命令在执行之前是为替换而处理的,而不是行

这意味着在设置
remotemachine
变量之前,整个
if()else()
结构将对其进行替换,这意味着当您想要使用它时,它不会被设置为您想要的

例如,此代码:

@echo off
set xyzzy=plugh
if a==a (
    set xyzzy=twisty
    echo %xyzzy%
)
不会像您所想的那样输出
twisty
,而是为您提供
plugh

您需要查看延迟扩展和
扩展运算符:

@setlocal enableextensions enabledelayedexpansion
@echo off
set xyzzy=plugh
if a==a (
    set xyzzy=twisty
    echo !xyzzy!
)
endlocal

第三件事,我怀疑这是造成您当前问题的原因,请将
移动到
的末尾,如果
行:

if %remotemachinecheck%==local (
使用下一行上的
),它会生成如下错误:

The syntax of the command is incorrect.
但这里有一件棘手的事情:它在
if
行之前输出,这可能会让你相信错误在前一行,根据以下记录(缩进计算机生成的行):


或者将
@echo-off
更改为
@echo-on
@user2033427,如果用户自定义命令shell使
echo-off
成为默认值(例如使用
cmd/q
),则可能更安全但我从未在野外看到过这一点。它仍然是一个有效的选项,因此我将把它合并到答案中。即使我修改了代码以仅使用
remotemachine
(没有改变的重复变量),我仍然有这个问题,所以我相信这是一个不同的问题。尽管我非常感谢大家的提醒,因为这显然也是一个问题。@Epic:而且,当你打开回音时,哪一行准确地显示了问题?我之前已经关闭了回音,@paxdiablo。它在收到来自的输入后通知我语法错误
set/P remotemachine=键入要查看其进程的远程计算机的名称(或为本地计算机键入local),然后按Enter键。
c:\pax> type qq1.cmd
    set q=w
    if a==a
    (
        echo yes
    )

c:\pax> qq1

    c>\pax> set q=w
    The syntax of the command is incorrect.

    c:\pax> if a==a

c:\pax> type qq2.cmd
    set q=w
    if a==a (
        echo yes
    )

c:\pax> qq2

    c>\pax> set q=w

    c:\pax> if a==a (echo equal )
    equal