简体   繁体   English

WMIC命令不适用于批处理文件的For循环

[英]WMIC Command Not Working in For Loop of Batch File

I have a batch file that I am trying to run, but I keep getting an error. 我有一个我试图运行的批处理文件,但我一直收到错误。 I think that this question is similar to I can't get the right syntax to use WMIC in batch file , but dbenham's answer doesn't completely work in my case because I am piping to findstr. 我认为这个问题类似于我无法在批处理文件中使用WMIC的正确语法 ,但dbenham的答案在我的情况下并不完全有效,因为我正在使用findstr。 Here is a part of the batch file (the part it is hanging on): 这是批处理文件的一部分(它挂在的部分):

for /F %%I in ('wmic nic where 'Manufacturer!="Microsoft" and Macaddress IS NOT NULL' get index ^| findstr /r [0-9]') do ( echo %%I ) for / F %% I in('wmic nic where'Manufacture!=“Microsoft”和Macaddress IS NOT NULL'get index ^ | findstr / r [0-9]')do(echo %% I)

The wmic command works just fine if you run it from cmd or it's own line of a batch file, but I cannot get it to run in the for loop. 如果你从cmd或它自己的批处理文件行运行它,wmic命令工作得很好,但我不能让它在for循环中运行。 Can anyone help me out here? 有人可以帮我从这里出去吗?

Thanks, John 谢谢,约翰

Try it this way: 试试这种方式:

for /f "tokens=2 delims==" %%I in (
    'wmic nic where "manufacturer!=\"Microsoft\" and macaddress is not null" get macaddress /format:list 2^>NUL'
) do echo %%I

You have to backslash-escape your quoted stuff where your quotes are nested. 你必须反斜杠 - 转义引号嵌套的引用内容。

There is a feature in WMIC that it sometimes waits for user input. WMIC中有一个功能,它有时会等待用户输入。

If you replace 如果你更换

wmic

in your example, with 在你的例子中,与

echo. ^| wmic

it will allow the command to complete 它将允许命令完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM