CMD.exe:获取第二列输出到变量 [英] CMD.exe: Get second column output to variable
问题描述
我需要下面的命令第二列输出<USERNAME>
结果到变量:
I need the below command second column output <USERNAME>
result to variables:
query sessions|find "Active"
>console <USERNAME> 1 Active
我知道%USERNAME%
或whoami
可以获取当前用户名,但是此脚本将使用管理员帐户运行,并且需要能够捕获当前的活动登录用户名.如果我可以选择输出结果的第二列并将其分配给变量,则将有很大帮助.
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.
推荐答案
在这种情况下,您应该使用FOR /F
捕获输出
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
)
它还会在空格和TAB处拆分行,但是如果用户名包含空格,则可能会出现问题.
It splits also the line at spaces and TABs, but this can be problematic if the username contains spaces.
这篇关于CMD.exe:获取第二列输出到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!