如何在命令提示符下将整个输出发送到日志文件以显示时间更改和错误

时间:2019-01-29 16:12:38

标签: windows batch-file cmd io-redirection

以下代码通过处理每个命令并等待来完成我需要的工作。但是,我似乎无法弄清楚如何将所有输出输出到日志文件中。现在,它仅记录日期和时间。我需要将cmd中的所有输出(包括任何错误)记录到日志文件中。我将不胜感激。

echo Logged time = %time% %date%>> TaskSetBatchScripts.log

@echo off
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitor
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitorToastTask
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyRefreshTask
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN "\Microsoft\Windows\Power Efficiency Diagnostics\AnalyzeSystem"
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsToastTask
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsUpdateTask
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN "\Microsoft\Windows\Mobile Broadband Accounts\MNO Metadata Parser"
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN "\Microsoft\Windows\Windows Error Reporting\QueueReporting"
timeout 5 > NUL
echo %time%

schtasks /Change /disable/TN \Microsoft\Windows\Defrag\ScheduledDefrag
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN "Microsoft\Windows\Windows Media Sharing\UpdateLibrary"
timeout 5 > NUL
echo %time%

schtasks /Change /disable /TN Microsoft\Windows\Maintenance\WinSAT 
timeout 5 > NUL
echo %time%

2 个答案:

答案 0 :(得分:1)

实现这一点非常容易。有两种方法:在批处理文件中进行操作:

setlocal EnableDelayedExpansion
(
echo Logged time = !time! !date!

@echo off
echo !time

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitor
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyMonitorToastTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Shell\FamilySafetyRefreshTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Power Efficiency Diagnostics\AnalyzeSystem"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsToastTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maps\MapsUpdateTask
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Mobile Broadband Accounts\MNO Metadata Parser"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Windows Error
Reporting\QueueReporting"
timeout 5 > NUL
echo !time!

schtasks /Change /disable/TN \Microsoft\Windows\Defrag\ScheduledDefrag
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN "\Microsoft\Windows\Windows MediaSharing\UpdateLibrary"
timeout 5 > NUL
echo !time!

schtasks /Change /disable /TN \Microsoft\Windows\Maintenance\WinSAT 
timeout 5 > NUL
echo !time!
)>>TaskSetBatchScripts.log 2>&1

在此处不确定您需要什么:追加(>>)或重定向(>)[擦除以前的内容]。

第二种方法是从cmd运行文件,如下所示:

(filename.bat)>>TaskSetBatchScripts.log 2>&1

再次不确定您需要什么。

(第三种方法是将每个命令的输出附加到添加2>&1的文件中。)

答案 1 :(得分:0)

  

我认为您需要将%time%更新为!time!,因此添加 setlocal EnableDelayedExpansion

因此,对于您的特定情况,我建议制作以下批处理文件:

@echo off & cd /d "%~dp0" & setlocal enabledelayedexpansion 
(echo Logged time = !time! %date% & for /f "tokens=* delims=*" %%i in (^'
schtasks /Change /disable /TN "\Microsoft\Windows\Shell\FamilySafetyMonitor" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Shell\FamilySafetyMonitorToastTask" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Shell\FamilySafetyRefreshTask" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Power Efficiency Diagnostics\AnalyzeSystem" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Maps\MapsToastTask" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Maps\MapsUpdateTask" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Mobile Broadband Accounts\MNO Metadata Parser" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Windows Error Reporting\QueueReporting" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Defrag\ScheduledDefrag" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Windows Media Sharing\UpdateLibrary" ^& 
schtasks /Change /disable /TN "\Microsoft\Windows\Maintenance\WinSAT" ^& 
') do echo/%%i & echo !time! & timeout /t 5 > NUL)>> TaskSetBatchScripts.log