昨日分のイベントビューアのログをファイルにエクスポート

下記バッチをタスクスケジューラに仕込んで、毎朝エクスポートする。

@echo off
REM 
REM ※「管理者として実行」して下さい。
REM 

REM 日付の前日を求める
REM 日付はYYYY/MM/DD 形式とする。結果は環境変数 ans へ返す。
REM 引数がないときは本日とする。
if "%1"=="" (  
    for /F "tokens=1" %%a in ('date /t') do set orgdate=%%a
) else (
    set orgdate=%1
)

:年月日の分割
set yy=%orgdate:~0,4%
set mm=%orgdate:~5,2%
set dd=%orgdate:~8,2%
:月日の数値化(8進数対策)
set /a mm=1%mm%-100
set /a dd=1%dd%-101

if %dd% NEQ 0 goto end

:月跨り処理
set /a mm=mm-1
if %mm% EQU 0 set mm=12&&set /a yy=yy-1

echo set sub=0030101001010 >sub.bat
echo set /a dd=31-%%sub:~%mm%,1%% >>sub.bat
call sub.bat

:後処理
if exist sub.bat del sub.bat

:閏年処理
:4で割り切れるか?
  set /a u=yy %% 4
  if not %u%==0 goto end
:100で割り切れて、400で割り切れないか?
  set /a u=yy %% 400
  set /a v=yy %% 100
  if %v%==0 if not %u%==0 goto end
:2月か?
  if %mm% EQU 2 set /a dd=dd+1

:end
set mm=0%mm%
set mm=%mm:~-2%

set dd=0%dd%
set dd=%dd:~-2%

set /a dd1=1%dd%-101
set dd1=0%dd1%
set dd1=%dd1:~-2%

set ans=%yy%-%mm%-%dd%
set ans1=%yy%-%mm%-%dd1%

set ExportDir=C:\EventLogExport
if not exist %ExportDir% mkdir %ExportDir%

if exist %ExportDir%\Application%ans%.evtx del %ExportDir%\Application%ans%.evtx
set s1=wevtutil epl Application %ExportDir%\Application%ans%.evtx /q:"*[System[TimeCreated[@SystemTime>='%ans1%T15:00:00.000Z' and @SystemTime<='%ans%T14:59:59.999Z']]]"
%s1%

if exist %ExportDir%\Security%ans%.evtx del %ExportDir%\Security%ans%.evtx
set s1=wevtutil epl Security %ExportDir%\Security%ans%.evtx /q:"*[System[TimeCreated[@SystemTime>='%ans1%T15:00:00.000Z' and @SystemTime<='%ans%T14:59:59.999Z']]]"
%s1%

if exist %ExportDir%\System%ans%.evtx del %ExportDir%\System%ans%.evtx
set s1=wevtutil epl System %ExportDir%\System%ans%.evtx /q:"*[System[TimeCreated[@SystemTime>='%ans1%T15:00:00.000Z' and @SystemTime<='%ans%T14:59:59.999Z']]]"
%s1%

if exist %ExportDir%\Internet_Explorer%ans%.evtx del %ExportDir%\Internet_Explorer%ans%.evtx
set s1=wevtutil epl "Internet Explorer" %ExportDir%\Internet_Explorer%ans%.evtx /q:"*[System[TimeCreated[@SystemTime>='%ans1%T15:00:00.000Z' and @SystemTime<='%ans%T14:59:59.999Z']]]"
%s1%

:echo ans=%ans%
:echo ans1=%ans1%
:echo %s1%
:pause

【バッチで前日の日付を求める】
http://homepage1.nifty.com/jak/batch/date.html

【日付の頭に0が付くと8進数と認識するので注意が必要】
http://februaly.exblog.jp/13858864

【wevtutilでの日時指定】
イベントビューアの[現在のログをフィルター]にて、[ログの日付] > [ユーザー設定の範囲] > 開始日と終了日を[次の日時]で指定し、XMLタブをクリックすると表示されている日時指定をコピーする。

本日が2013-08-09の場合、前日の00:00:00〜23:59:59は、下記の指定となる。
wevtutil epl Security C:\secLog2013-08-08.evtx /q:"*[System[TimeCreated[@SystemT
ime>='2013-08-07T15:00:00.000Z' and @SystemTime<='2013-08-08T14:59:59.999Z']]]"