Listing04_Event_Notification (Perl)

This code can be found in Chapter 5 of Managing Enterprise Active Directory Services

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# From the book "Managing Enterprise Active Directory Services"
# ISBN: 0-672-32125-4

use Win32::OLE;
eval {
   $wmi_path = "winmgmts:{(security)}";
   $wql_query = " select *
                    from __instancecreationevent
                   where targetinstance isa 'Win32_NTLogEvent'
                     and targetinstance.LogFile = 'System'
                     and targetinstance.EventIdentifier = 5778 ";
   $events = Win32::OLE->GetObject($wmi_path)->ExecNotificationQuery(
                                                          $wql_query);
   die "Could not create event\n" unless ref $events;

   print "\nWaiting for NT event...\n\n";

   while (1) {
      $NTEvent = $events->NextEvent();
      print "Log:      ",$NTEvent->{TargetInstance}->{LogFile},"\n";
      print "EventID:  ",$NTEvent->{TargetInstance}->{EventIdentifier},"\n";
      print "Message:  ",$NTEvent->{TargetInstance}->{Message} || "<none>","\n";
      print "Time:     ",$NTEvent->{TargetInstance}->{TimeGenerated},"\n\n";
    }
};
if ($@) {
   print "Error: $@\n",Win32::OLE->LastError, "\n";
}

This code has been viewed 663 times.

New from the creators of TechTasks.com: StatSheet.com