I am making a batch dos code to execute commands and capture code in files, while also keeping the output on screen.
EDIT2: 3 apr 21, 15:36 The commands that I want execute are slow and fast and some commands can only be executed once. I have to execute for example commands like chkdsk, dism, sfc, wmic, dos (internal, external), powershell.
I thought the fastest way was to use the powershell "tee".
ex. (echo foo & echo bar) | powershell "$input | tee -a out.txt"
C:\WINDOWS\system32>(echo foo & echo bar) | powershell "$input | tee -a out.txt"
foo
bar
but when i use it with wmic i have different results:
C:\WINDOWS\system32>wmic recoveros get autoreboot
AutoReboot
FALSE
C:\WINDOWS\system32>wmic recoveros get autoreboot | powershell "$input | tee -a out.txt"
AutoReboot
FALSE
C:\WINDOWS\system32>
- Why?
- Is there a way to correct without breaking the code?
EDIT1: 3 apr 21, 14:46
I add some information how suggest JosefZ:
C:\WINDOWS\system32>del out.txt out2.txt
C:\WINDOWS\system32>wmic recoveros get autoreboot > out2.txt
C:\WINDOWS\system32>wmic recoveros get autoreboot | powershell "$input | tee -a out.txt"
AutoReboot
FALSE
C:\WINDOWS\system32>type out.txt
AutoReboot
FALSE
C:\WINDOWS\system32>type out2.txt
AutoReboot
FALSE
C:\WINDOWS\system32>powershell -nopro "(cat out.txt -encoding Byte) -join ' '"
255 254 65 0 117 0 116 0 111 0 82 0 101 0 98 0 111 0 111 0 116 0 32 0 32 0 13 0 10 0 13 0 10 0 70 0 65 0 76 0 83 0 69 0 32 0 32 0 32 0 32 0 32 0 32 0 32 0 13 0 10 0 13 0 10 0 13 0 10 0 13 0 10 0
C:\WINDOWS\system32>powershell -nopro "(cat out2.txt -encoding Byte) -join ' '"
255 254 65 0 117 0 116 0 111 0 82 0 101 0 98 0 111 0 111 0 116 0 32 0 32 0 13 0 10 0 70 0 65 0 76 0 83 0 69 0 32 0 32 0 32 0 32 0 32 0 32 0 32 0 13 0 10 0
C:\WINDOWS\system32>
EDIT3: 3 apr 21, 16:05 This is the code that I developed for the moment (some commands are commented for the moment with "rem"):
for %%C in (
"rem sfc /scannow"
"rem dism /online /cleanup-image /scanhealth"
"rem dism /online /cleanup-image /restorehealth"
"rem sfc /scannow"
"rem"
"rem chkdsk /scan"
"rem"
"wmic recoveros get autoreboot"
"wmic recoveros set autoreboot = false"
"wmic recoveros get autoreboot"
"wmic recoveros get DebugInfoType"
"wmic recoveros set DebugInfoType = 7"
"wmic recoveros get DebugInfoType"
"rem"
"wmic pagefile list /format:list"
"wmic Computersystem where name="%COMPUTERNAME%" get AutomaticManagedPagefile"
"wmic Computersystem where name="%COMPUTERNAME%" set AutomaticManagedPagefile=True"
"wmic Computersystem where name="%COMPUTERNAME%" get AutomaticManagedPagefile"
"rem"
"bcdedit /enum {badmemory}"
) do ( %ComSpec% /c "echo(%CD%^>%%~C & %%~C " | powershell -command " $input | tee -Append test.txt ")
EDIT4: 10 apr 2021, 8:21 I add this code to create a reference output to facilitate the comparison process between the various tests. This output is to be considered both on screen and on file.
@echo off
rem Warning! Run as administrator.
rem go safe place for testing...
cd %temp%
for %%C in (
"rem sfc /scannow"
"rem dism /online /cleanup-image /scanhealth"
"rem dism /online /cleanup-image /restorehealth"
"rem sfc /scannow"
"rem chkdsk /scan"
"wmic recoveros get autoreboot"
"wmic recoveros set autoreboot = false"
"wmic recoveros get autoreboot"
"wmic recoveros get DebugInfoType"
"wmic recoveros set DebugInfoType = 7"
"wmic recoveros get DebugInfoType"
"wmic pagefile list /format:list"
"wmic Computersystem where name="%COMPUTERNAME%" get AutomaticManagedPagefile"
"wmic Computersystem where name="%COMPUTERNAME%" set AutomaticManagedPagefile=True"
"wmic Computersystem where name="%COMPUTERNAME%" get AutomaticManagedPagefile"
"bcdedit /enum {badmemory}"
) do (
rem Partially simulates "echo on" on commands.
echo(%CD%^>%%~C
rem Execute command.
%%~C
rem alternative, but opens another cmd process.
rem %ComSpec% /c "echo(%CD%^>%%~C & %%~C"
)
pause
goto :eof