CMD.exe: Get second column output to variable

孤街浪徒 提交于 2020-05-25 08:09:52

问题


I need the below command second column output <USERNAME> result to variables:

query sessions|find "Active"
>console    <USERNAME>     1   Active

I know the %USERNAME% OR whoami could get the current user name but this script will run using administrator account and will need to be able to capture the current active logon username. If I could select the second column of the output results and assign it to a variable.. this will be a great help.


回答1:


In this case you should use FOR /F to capture the output

for /F "tokens=1,2,3,4,5" %%A in ('"query session | find "Active""') DO (
  echo %%A,%%B,%%C,%%D,%%E
)

It splits also the line at spaces and TABs, but this can be problematic if the username contains spaces.



来源:https://stackoverflow.com/questions/19442819/cmd-exe-get-second-column-output-to-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!