我需要根据电池电量变化在任务计划程序中创建任务
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
则将在APPLICATION事件日志中生成一个ID为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语法
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"
进一步阅读