ab’s blog

インフラの呟きです。

Windows11 テキストの内容をイベントログに登録

はじめに

powershellでイベントログに書き込むサンプル。(実行にはadministrator権限(管理者)が必要で、無いとイベントログ書き込みがエラーになります)

powershellサンプル

sample.ps1

try {
  #テキストファイルを読み込み
  $file_data = Get-Content .\text.txt -Raw -ErrorAction Stop
} 
catch {
  echo ERROR:$error
}
#イベント書き込み
eventcreate /t ERROR /id 100 /l application /so sample /d "$file_data" 

text.txtの中身

START
これはテスト
END

実行結果

#コマンドプロンプトから実行 ※administrator権限(管理者)が必要
>powershell -ExecutionPolicy RemoteSigned .\sample.ps1

イベントログに登録されました。

イベントログ

以上