我有一個批處理腳本,它的一部分正在清除 Windows 10 的事件日志。我想看到以下wevtutil.exe命令的輸出,但只在一行上輸出(覆寫每一行)而不是多行.
我知道 ANSI 轉義序列ESC[1FESC[0J,ESC[1A它可以覆寫前一行(ESC字符是 ALT 027,可以在 Notepad 中看到),但我無法弄清楚如何使用wevtutil.exe el命令來做到這一點。它仍然一個接一個地輸出每一行。這是我嘗試過的(以管理員權限在 CMD 中運行):
@echo off
SET OverwriteLine=ESC[1FESC[0J
echo Making sure the variable OverwriteLine actually works. This is line 1.
echo %OverwriteLine%If you see this line instead of line 1, OverwriteLine works.
echo Great, now let^'s see if it works for the "wevtutil.exe cl" command
pause
echo Clearing Event logs...
@echo on
for /F "tokens=*" %%E in ('wevtutil.exe el') DO echo %OverwriteLine% | wevtutil.exe cl "%%E"
pause
我知道這可以通過 Powershell 來完成$host.ui.RawUI.CursorPosition,但我需要一個 CMD/BAT 解決方案。
uj5u.com熱心網友回復:
由于我們針對每個問題處理一個特定問題,而您的主要問題似乎與 VT100 序列的實作有關,因此這里是一個使用完全不同的 for 回圈的注釋示例,僅用于演示該技術。
@Echo Off
SetLocal EnableExtensions
Rem Create a variable to use as the escape character
For /F %%G In ('Echo Prompt $E ^| %SystemRoot%\System32\cmd.exe') Do Set "\x1b=%%G"
Rem This is a static line included to demonstrate that it is unaffected
Echo(Output
Rem Turns off the cursor and removes a stray new line caused by Echo
Echo(%\x1b%[?25l%\x1b%[1A
Rem For loop example
For /L %%G In (1,1,8) Do (
Rem Clears to the beginning of the line, prints the output, and moves up a line
Echo(%\x1b%[1KLine %%G%\x1b%[1A
Rem Creates a short delay between output lines to see the effect
%SystemRoot%\System32\PATHPING.EXE 127.0.0.1 -n -q 1 -p 650 1>NUL
)
Rem Turns on the cursor again
Echo(%\x1b%[?25h
Pause
uj5u.com熱心網友回復:
好的,找到了解決辦法:
for /F "tokens=*" %%E in ('wevtutil.exe el') DO (wevtutil.exe cl "%%E" | echo <ESC>[1F<ESC>[0KClearing %%E)
說明/注意事項:
<ESC>表示特殊的
ESC轉義碼序列。如果您在Notepad 中編輯代碼,則可以通過在Notepad 中鍵入ALT 027來生成此字符,或者在運行時使用Compo提到的 FOR 回圈生成它。我們用
ESC[1F將游標移動到上一行的開頭。然后我們用
ESC[0K清除游標到行尾。清除 Windows 事件日志需要以管理員權限運行 CMD 腳本。
預計某些事件日志會失敗。失敗的將保留在螢屏上,每個都在一個新行上(這可能會變得方便)。如果您不想看到任何失敗,只需添加2>nul:
DO (wevtutil.exe cl "%%E" 2>nul | echo <ESC>[1F<ESC>[0KClearing %%E)
您可以在此處了解有關轉義碼的更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/335948.html
