
我需要在 TS 中建立一個基於電池電量變化的任務。假設我的電池電量從67%到66%。如何根據此事件運行任務。 Windows 會記錄這個嗎?我在任何地方都找不到這個資訊。
答案1
我需要根據電池電量變化在任務規劃程式中建立一個任務
Windows 不會將此類詳細資訊記錄為事件。但是,您可以使用下面的批次檔之類的內容來建立自訂事件。
電池.cmd
此批次檔監控目前電池電量百分比,並在電量低於使用者定義的閾值時建立使用者定義的事件。
@echo off
setlocal EnableDelayedExpansion
rem set threshold value
set _threshold=82
:start
rem get the battery charge
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1" %%i in (`wmic Path Win32_Battery Get EstimatedChargeRemaining ^| findstr /r /v "^$"`) do (
set _charge=%%i
echo !_charge!
if !_charge! lss !_threshold! (
echo threshold reached
rem create a custom event in the application event log
rem requires administrator privileges
eventcreate /l APPLICATION /t WARNING /ID 999 /D "Battery charge has dropped"
goto :done
) else (
rem wait for 10 minutes then try again
timeout /t 600 /nobreak
goto :start
)
)
:done
endlocal
筆記:
- 此
Eventcreate
指令適用於 Windows XP 直至 Windows 10,需要管理員權限才能運作 _threshold
按要求設定- 如果電池電量低於此值,
999
將在應用程式事件日誌中產生帶有 ID 的事件及其描述Battery charge has dropped
eventcreate
根據您的具體情況修改命令。timeout
根據您的情況需要修改延遲。
輸出範例:
我的電池目前電量為 81%。我將閾值設為82
。這是我跑步時發生的情況Battery.cmd
:
> battery
81
threshold reached
SUCCESS: An event of type 'WARNING' was created in the 'APPLICATION' log with 'EventCreate' as the source.
這是事件日誌中的新條目:
事件創建語法
EVENTCREATE [/S system [/U username [/P [password]]]] /ID eventid
[/L logname] [/SO srcname] /T type /D description
Description:
This command line tool enables an administrator to create
a custom event ID and message in a specified event log.
Parameter List:
/S system Specifies the remote system to connect to.
/U [domain\]user Specifies the user context under which
the command should execute.
/P [password] Specifies the password for the given
user context. Prompts for input if omitted.
/L logname Specifies the event log to create
an event in.
/T type Specifies the type of event to create.
Valid types: SUCCESS, ERROR, WARNING, INFORMATION.
/SO source Specifies the source to use for the
event (if not specified, source will default
to 'eventcreate'). A valid source can be any
string and should represent the application
or component that is generating the event.
/ID id Specifies the event ID for the event. A
valid custom message ID is in the range
of 1 - 1000.
/D description Specifies the description text for the new event.
/? Displays this help message.
Examples:
EVENTCREATE /T ERROR /ID 1000
/L APPLICATION /D "My custom error event for the application log"
EVENTCREATE /T ERROR /ID 999 /L APPLICATION
/SO WinWord /D "Winword event 999 happened due to low diskspace"
EVENTCREATE /S system /T ERROR /ID 100
/L APPLICATION /D "Custom job failed to install"
EVENTCREATE /S system /U user /P password /ID 1 /T ERROR
/L APPLICATION /D "User access failed due to invalid user credentials"
進一步閱讀
- Windows CMD 命令列的 AZ 索引- 與 Windows cmd 行相關的所有內容的絕佳參考。
- 事件創建- 在 Windows 事件檢視器中建立自訂事件。
- 計劃任務- 建立/編輯計畫作業/任務。可以在本機或遠端電腦上建立作業。
- 沃米克- Windows 管理規範命令。
答案2
答案3
好吧,DavidPostill 提供的腳本不起作用。這是一個不錯的小腳本,但程式碼要么不穩定,要么過時。
這是固定的:
@echo off
setlocal EnableDelayedExpansion
rem set threshold value
set _threshold=30
:start
rem get the battery charge
rem use findstr to strip blank lines from wmic output
for /f "usebackq skip=1 tokens=1" %%i in (`wmic Path Win32_Battery Get EstimatedChargeRemaining ^| findstr /r /v "^$"`) do (
set _charge=%%i
echo !_charge!
if !_charge! lss !_threshold! (
echo threshold reached
rem create a custom event in the application event log
rem requires administrator privileges
eventcreate /l APPLICATION /t WARNING /ID 999 /D "Battery charge has dropped below the threshold."
goto :done
) else (
rem wait for 1 minute then try again
timeout /t 60 /nobreak
goto :start
)
)
:done
endlocal
我建議對 DavidPostill 的答案進行編輯,但我不知道為什麼它沒有被批准...
答案4
有一種更簡單的方法來檢查電池電量。在導航區域中,只需將滑鼠放在電池圖示上,它就會給出百分比。