电池电量变化的 Windows 事件 ID

Rid*_*his 9 windows windows-7 battery events windows-10

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

Dav*_*ill 11

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

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
Run Code Online (Sandbox Code Playgroud)

笔记:

  • Eventcreate命令适用于 Windows XP 和包括 Windows 10,它需要管理员权限才能工作
  • _threshold根据需要设置
  • 如果电池电量低于此值,999将在 APPLICATION 事件日志中生成一个带有 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.
Run Code Online (Sandbox Code Playgroud)

这是事件日志中的新条目:

在此处输入图片说明


事件创建语法

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"
Run Code Online (Sandbox Code Playgroud)

进一步阅读

  • Windows CMD 命令行的 AZ 索引- Windows cmd 行相关的所有内容的极好参考。
  • eventcreate - 在 Windows 事件查看器中创建自定义事件。
  • schtasks - 创建/编辑计划作业/任务。可以在本地或远程计算机上创建作业。
  • wmic - Windows 管理工具命令。


mag*_*981 6

有一个事件 ID 为 13的Microsoft-Windows-BatteryETW 提供程序BatteryPercentRemaining。您可以编写一个使用TraceEvent为该提供程序创建实时侦听器的项目Microsoft-Windows-Battery。该事件具有RemainingPercentage显示状态和PercentageChange查看更改的条目:

在此处输入图片说明

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