- WMI Fun !! -|WMI StepWMI SampleSearchWMI Fun !! GroupsAbout WMI Fun !!
Top Page >> WMI Step >> イベントログの内容を保存する
イベントログの内容を保存する
Topics

イベントログの内容を保存するサンプルです。Win32_NTLogEventFileクラスを使用します。


Sample Code

Private Sub Command1_Click()

Dim EveSet As SWbemObjectSet
Dim Eve As SWbemObject
Dim Locator As SWbemLocator
Dim Service As SWbemServices
Dim Security As SWbemSecurity
Dim Ret As Long
Const wbemPrivilegeBackup = 16

Set Locator = New WbemScripting.SWbemLocator
Set Security = Locator.Security_
Security.Privileges.Add wbemPrivilegeBackup
Set Service = Locator.ConnectServer

Set EveSet = Service.ExecQuery _
    ("Select * from Win32_NTEventLogFile where LogFileName='Application'")

For Each Eve In EveSet

    Ret = Eve.BackupEventLog("c:\applicationlog.evt")
  
Next

If Ret = 0 Then
    MsgBox "アプリケーションログの保存に成功しました。"
Else
    MsgBox "アプリケーションログの保存に失敗しました。"
End If

Set EveSet = Nothing
Set Eve = Nothing
Set Locator = Nothing
Set Service = Nothing
Set Security = Nothing

End Sub

Comment

実行するとイベントログの中のアプリケーションログをcドライブに保存します。もちろんアプリケーションログ以外にもソースを変更すれば保存可能です。LogFileNameプロパティをシステムログであれば「System」、セキュリティログであれば「Security」を指定します。

以前PCをシャットダウンサンプルで紹介したとおり今回の保存行為にも特権が必要です。wbemPrivilegeBackupをバックアップを行う特権があることを明示的に指定し、WMIへ接続を行っています。


WMI を本で勉強したい方へ。 Windows PowerShellインアクション を読んでみては?
なかなか WMI のことが乗っている書籍は少ないですが…管理系業務に係る方には楽しめるかも?
パソコンで目が疲れたら、ブルーベリーアイ
『ブルーベリーアイ』1粒中ビルベリーエキス160mg【楽天シニア市場】
 ^ Copyright(C) 1997-2008 とお All rights reserved. <<