windows - 如何在命令提示符下将整个输出发送到日志文件以显示时间更改和错误
问题描述
以下代码通过处理每个命令并等待来完成我需要做的事情。但是,我似乎无法弄清楚如何将所有输出放入日志文件。现在它只记录日期和时间。我需要将 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%
解决方案
实现这一点,非常容易。有两种方法:在批处理文件中进行:
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
)。
推荐阅读
- python-3.x - 使用 Python Boto3 在 Route53 的托管区域中更改记录集权重
- javascript - 如何在 React 中为一个请求保存多个表单?
- php - 致命错误:GRP php 扩展中超过了 30 秒的最大执行时间
- c# - 没有 GA/GC 的千分之一秒字符串的最佳获取
- php - Sonata 管理包,如果用户不选择任何过滤器,如何不显示任何结果并且不对 DB 执行任何查询?
- oracle - 以 UTF8 格式从 oracle 数据库中导出 CLOb 数据
- c# - 在哪里实例化处理 .NET Core 中的服务事件的类?
- javascript - Chart.js 折线图 x 轴标签舍入
- linux - 如何让 Linux 等到我的程序完成其 SIGTERM 操作?
- vue.js - asyncData() 在开发中到达,而不是在发布时到达