
バッテリー残量の変化に基づいて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 コマンド ラインに関連するあらゆることに関する優れたリファレンス。
- イベント作成- Windows イベント ビューアーでカスタム イベントを作成します。
- タスク- スケジュールされたジョブ/タスクを作成/編集します。ジョブは、ローカル コンピューターまたはリモート コンピューターで作成できます。
- wmic- Windows Management Instrumentation コマンド。
答え2
Microsoft-Windows-Battery
ID 13のイベントを持つETWプロバイダーがあります。BatteryPercentRemaining
これを使用するプロジェクトをコーディングできます。トレースイベント作成するリアルタイムリスナーこの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
バッテリー レベルを確認するには、もっと簡単な方法があります。ナビゲーション領域で、バッテリー アイコンの上にマウスを置くと、パーセンテージが表示されます。