2017-07-17 91 views
0

我想創建一個Windows批量登錄腳本,收集每個用戶的系統信息和用戶名/計算機名,並將其輸出到一個日誌文件(而不是爲每個用戶創建一個單獨的文件)。所以,我需要這樣的東西echo %username%-%computername% >> file.txtsysteminfo|find /i "original" >> file.txt,我管理的是這樣做的:輸出systeminfo登錄腳本

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 

每當有人登錄時,它輸出到一個文件,但該用戶名和系統的系統信息是分開的。我需要他們輸出到單獨的列,例如:

Username-ComputerName Original Install Date 
Jo-PC1     17/16/2016, 09:14:34 
Dan-PC2     17/03/2015, 11:00:05 

回答

0

請注意,堆棧溢出不是一個寫代碼爲我服務。不管怎麼說,我可恥的MC ND在我question


@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 

的解釋已經是腳本里面偷走了腳本(and edited),所以我不會解釋太多。

  • 只是得到的結果爲2個文件
  • 回聲在一起

輸出部分

rem All done, leave 
cls 
type File.ext 
pause 
goto :eof 

所以你可能需要一些調整。