我正在尝试创建一个Windows批处理登录脚本,该脚本收集每个用户的系统信息和用户名/计算机名称,并将其输出到一个日志文件(而不是为每个用户创建的单独文件)。所以我需要像echo %username%-%computername% >> file.txt
和systeminfo|find /i "original" >> file.txt
这样的东西,我设法用这样的东西:
systeminfo|find /i "original" >> \\share\sysinfo.log
echo %username%-%computername% >>\\share\user.log
type \\share\user.log > \\share\Results\systeminfo.log
type \\share\sysinfo.log >> \\share\Results\systeminfo.log
每次有人登录时,都会输出到一个文件,但用户名和systeminfo是分开的。我需要它们输出到单独的列中,例如:
Username-ComputerName Original Install Date
Jo-PC1 17/16/2016, 09:14:34
Dan-PC2 17/03/2015, 11:00:05
答案 0 :(得分:0)
请注意,Stack Overflow不是针对我的写代码服务。无论如何,我可耻地在我的question
中偷走了MC ND的脚本(and edited)
@echo off
setlocal enableextensions disabledelayedexpansion
rem Prepare padding for two column
set "Width=30"
setlocal enabledelayedexpansion
set "padding= "
for /l %%a in (1 1 %Width%) do set "padding=!padding! "
endlocal & set "padding=%padding%"
rem return "Username-ComputerName Original Install Date"
echo Username-ComputerName Original Install Date>File.ext
rem Prepate two files to store username-computerName and install date lists
for %%d in ("%temp%\username-computerName.%random%%random%%random%.tmp") do (
for %%f in ("%temp%\installName.%random%%random%%random%.tmp") do (
rem Retrieve the list of username-computerName and install date into the temp files
type user.log > "%%~fd" 2>nul
type date.log > "%%~ff" 2>nul
rem Assign each temporary file to a separate stream
rem and call the code to process them
9<"%%~fd" 8<"%%~ff" call :dumpPairs
rem Clean up temporary files
) & del /q "%%~ff"
) & del /q "%%~fd"
rem All done, leave
cls
type File.ext
pause
goto :eof
rem Read stream 9 to retrieve username-computerName - Clean var on failure
rem Read stream 8 to retrieve install date - Clean var on failure
rem If both variables are uninitialized, there is nothing more to do
rem Concatenate both information and padding
rem Echo the padded results
rem Repeat the process
:dumpPairs
<&9 set /p "username-computerName=" || set "username-computerName="
<&8 set /p "installName=" || set "installName="
if not defined username-computerName if not defined installName goto :eof
set "line=%username-computerName%%padding%"
setlocal enabledelayedexpansion
echo(!line:~0,%Width%! !installName!>>File.ext
endlocal
goto :dumpPairs
解释已经在脚本中,所以我不会解释太多。
输出部分是
rem All done, leave
cls
type File.ext
pause
goto :eof
因此您可能需要进行一些调整。