0

How can I output each command lines to one single file but keep monitoring the results at the same command.

@echo off
Title %~n0

if not "%1" == "max" start /MAX cmd /c %0 max & exit/b

Echo Hard Disk Info
set record="C:\%computername%.txt"
Echo.
powershell "get-physicaldisk">C:\%computername%.txt>con
echo=================================
Echo.
Echo CPU Info
Echo.
wmic cpu get caption, name
echo=================================
Echo.
Echo RAM Info
Echo.
wmic memorychip get capacity,memorytype,speed,typedetail,manufacturer
echo=================================
echo.
Echo Windows Version
Echo.
systeminfo | findstr /B /i /C:"OS Name" /C:"OS Version" /C:"System Type" /C:"System Locale" /C:"Input Locale"

echo=================================
Echo.
Echo Office Version
echo.
Echo LCID = 1033-English(US) 
wmic product where "Name like '%%Office%%'" get language,name, version
Pause

@exit %0
Stephan
  • 53,940
  • 10
  • 58
  • 91
VinBest
  • 3
  • 3
  • [awkward using pure batch](https://stackoverflow.com/questions/15551379/how-do-i-make-a-log-of-all-echo-commands-in-a-batch-file). Might not be exactly what you want. If downloading a third-party program is acceptable, google for `tee for windows`. (PowerShell has the `tee-object` cmdlet) – Stephan Jan 02 '22 at 11:06

2 Answers2

0

The following code could be used to write everything into a file and display also in the console window:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
title %~n0
if not "%~1" == "max" start /MAX %SystemRoot%\System32\cmd.exe /D /C %0 max & exit /B

set "RecordFile=%UserProfile%\%ComputerName%.txt"
del "%RecordFile%" 2>nul
set "TempFile=%TEMP%\%~n0.tmp"

call :OutputInfo "Hard Disk Info"
call :OutputData %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe "Get-PhysicalDisk"

call :OutputInfo "CPU Info"
call :OutputData %SystemRoot%\System32\wbem\wmic.exe CPU GET Caption,Name

call :OutputInfo "RAM Info"
call :OutputData %SystemRoot%\System32\wbem\wmic.exe MEMORYCHIP GET Capacity,MemoryType,Speed,TypeDetail,Manufacturer

call :OutputInfo "Windows Version"
call :OutputData %SystemRoot%\System32\cmd.exe /D /S /C "%SystemRoot%\System32\systeminfo.exe 2>nul | %SystemRoot%\System32\findstr.exe /B /I /C:"OS Name" /C:"OS Version" /C:"System Type" /C:"System Locale" /C:"Input Locale""

call :OutputInfo "Microsoft Office Version"
echo LCID 1033 = English (US)>>"%RecordFile%"
echo LCID 1033 = English (US)
call :OutputData %SystemRoot%\System32\wbem\wmic.exe PRODUCT where "Name like '%%%%Microsoft Office%%%%'" GET Language,Name,Version

del "%TempFile%" 2>nul
echo(
pause
exit /B

:OutputData
%* >"%TempFile%"
for %%I in ("%TempFile%") do if %%~zI == 0 goto :EOF
type "%TempFile%">>"%RecordFile%"
type "%TempFile%"
goto :EOF

:OutputInfo
if not exist "%RecordFile%" goto InfoOutput
(echo =================================& echo()>>"%RecordFile%"
echo =================================
echo(
:InfoOutput
(echo %~1&echo()>>"%RecordFile%"
echo %~1
echo(
goto :EOF

Note 1:

The PowerShell command line outputs on Windows 7 with by default installed PowerShell 2.0 just the following error message because of the cmdlet Get-PhysicalDisk is not available with PowerShell 2.0.

The term 'Get-PhysicalDisk' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:17
+ Get-PhysicalDisk <<<< 
    + CategoryInfo          : ObjectNotFound: (Get-PhysicalDisk:String) [], Co 
   mmandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Note 2:

WMIC outputs data always Unicode encoded using UTF-16 Little Endian (two or four bytes per character) with byte order mark (BOM) while PowerShell and SystemInfo output data with just one byte per character. For that reason it is not advisable to directly output all data into one text file because of that would result in a text file using more than one character encoding making the file unreadable. Therefore each data output is first written into a temporary file always created new. The temporary file content is output next with command TYPE and the one byte per character encoded output is appended to the record file.

Note 3:

It is necessary to escape both % around Microsoft Office with three additional percent signs to pass %Microsoft Office% to wmic.exe executed in subroutine OutputData. Each % must be escaped with one more % to be interpreted as literal character in a batch file. But a command line with CALL is processed a second time by the Windows command processor. Therefore two more percent signs are necessary on both sides to get first %%Microsoft Office%% after first parsing of the command line and next %Microsoft Office% after the second parsing caused by command CALL.

Note 4:

The usage of just %Office% instead of %Microsoft Office% could result in output not really wanted like:

0         Microsoft Visual Studio 2010 Tools for Office Runtime (x64)       10.0.50908

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • cmd /?
  • del /?
  • echo /?
  • exit /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • powershell get-help get-physicaldisk
  • set /?
  • start /?
  • systeminfo /?
  • title /?
  • type /?
  • wmic /?
  • wmic cpu /?
  • wmic cpu get /? and Win32_Processor class
  • wmic memorychip /?
  • wmic memorychip get /? and Win32_PhysicalMemory class
  • wmic product /?
  • wmic product get /? and Win32_Product class

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143
0

There are very efficient small utilities such as windows versions of Tee or Tail that can help greatly in such situations. However, in the spirit of going commando one possibility is to wrap your existing batch file in a Power Shell emulation of Tee.

On Windows 7 I had a slight hiccup with your cmd file as get-physicaldisk is not recognized but it did not stall the output too much. See @mofi s Note 1.

Also note your end pause was not visible (due to this method of nesting) so I found replacing pause with

echo Press any key to exit & Pause>nul

worked better for me. Also your max option seems to be backwards since without max I get a larger output!

Anyway, assuming your cmd is "monitor.cmd" the following worked as slowly as expected.

powershell.exe -C "& {cmd /c 'monitor max' | tee -filepath monitor.log}"

to review output you can use

type monitor.log

K J
  • 8,045
  • 3
  • 14
  • 36