wmic进程 - 在变量中捕获

时间:2015-05-07 15:33:11

标签: batch-file wmic

我想在变量中捕获processID,但是我收到错误,谢谢......

wmic process where name="notepad.exe" get ProcessId

ProcessID

7948

for /f %%a IN ('wmic process where name="notepad.exe" get ProcessId') do set "MYVAL=%%a"
echo %MYVAL%

notepad.exe - Invalid alias verb

1 个答案:

答案 0 :(得分:3)

试试这个

for /f "usebackq" %%a IN (`wmic process where "name='notepad.exe'" get ProcessId`) do @echo %%a

修改

得到pid:

for /f "skip=1 usebackq" %%a IN (`wmic process where "name='notepad.exe'" get ProcessId`) do (

    rem @echo %%a
    if "%%a" neq "" set "pid=%%~a"

)

等号必须放在双引号中,因为它是批处理语法中的默认分隔符之一。