电池电量变化的 Windows 事件 ID

电池电量变化的 Windows 事件 ID

我需要在 TS 中创建一个基于电池电量变化的任务。假设我的电池电量从67%66%。我如何根据此事件运行任务。Windows 会记录此事件吗?我在任何地方都找不到此信息。

答案1

我需要根据电池电量变化在任务计划程序中创建一个任务

Windows 不会将此类详细信息记录为事件。不过,您可以使用类似下面的批处理文件并创建自定义事件。


电池命令

该批处理文件监视当前电池电量百分比,如果电量低于用户定义的阈值,则创建用户定义的事件。

@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 语法

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"

进一步阅读

答案2

有一个Microsoft-Windows-BatteryETW 提供BatteryPercentRemaining程序,其事件 ID 为 13。您可以编写一个使用追踪事件创建一个实时监听器Microsoft-Windows-Battery提供商的事件。事件包含RemainingPercentage显示状态和PercentageChange查看更改的条目:

在此处输入图片描述

当您看到此事件并看到-1的变化时PercentageChange,运行您想要的程序。

答案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

有一种更简单的方法来检查电池电量。在导航区域,只需将鼠标放在电池图标上,它就会显示百分比。

相关内容