出于资产吸收的目的,我试图批量创建一个脚本,该脚本运行在本地.txt文件中的计算机的计算机名称,让最后一个登录到该计算机的人员,并输出该脚本到格式为Machine Name: NAME | Last Login: USER
我打了一堵墙,我无法弄清楚发生了什么。机器名称正在输出,但最后一次登录'部分不是。这是我的代码:
@echo off
for /f %%a in (Assets.txt) do (
for /f %%b in ('wmic.exe /node:"%%a" computersystem get username') do set lastlogin=%%b
echo Machine Name: %%a Last Login: %lastlogin%)
pause
非常感谢任何帮助。谢谢。
更新2
for /f "delims=" %%a in (Assets.txt) do (
for /f "skip=1 delims=" %%b in (
'WMIC /Node:"%%a" ComputerSystem Get UserName 2^>nul'
) do for %%c in (%%b) do echo Host: %%a Username: %%b >>Usernames.txt)
答案 0 :(得分:0)
确实不需要设置变量,因此无需启用延迟扩展。
您可以将脚本更改为以下内容:
@Echo Off
For /F "Delims=" %%A In (Assets.txt) Do (
For /F "Skip=1 Delims=" %%B In (
'WMIC /Node:"%%A" ComputerSystem Get UserName 2^>Nul'
) Do For %%C In (%%B) Do Echo Machine Name: %%A Last Login: %%B)
Pause
你也可以这样:
@Echo Off
For /F "Skip=1 Delims=" %%A In ('
"WMIC /Node:@Assets.txt ComputerSystem Get Name, UserName"
') Do Call :Sub %%A
Pause
Exit /B
:Sub
If Not "%1"=="" Echo Machine Name: %1 Last Login: %2