Windows Perl Scripting Forums » Scripts
Problems with Win32::EventLog
(3 posts)-
Posted 5 years ago #
-
Let me try this again didn't realize it added html... to it. sorry.
I’ve been trying to use Win32::EventLog for a script on my win2k server and seem to have a problem when it generates an Event to the Event Viewer. The Event contains some extra garbage and I’m not sure how to generate the event without getting this garbage included in it.
Here’s the generated event message:
Event Type: Error
Event Source: Weekly Report Script
Event Category: None
Event ID: 100
Date: 09/26/2007
Time: 12:01:32 PM
User: N/A
Computer: LVEPL3LX620
Description:
The description for Event ID ( 100 ) in Source ( Weekly Report Script ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: This is a Test Error......I only want the description to contain the message “This is a Test Error…..”
Here’s the code. Can you please tell me what I’m doing wrong.
use Win32::EventLog;
my $Type = "ERROR";
my $IDNum = 100;
my $Description = "This is a Test Error.....";chomp( $Type, $IDNum, $Description );
my %Event = (
Length => NULL,
RecordNumber => NULL,
TimeGenerated => NULL,
EventID => $IDNum,
Category => NULL,
Source => "Weekly Report Script",
Strings => $Description,
);if ( uc($Type) eq "ERROR" ) {
$Event{'EventType'} = EVENTLOG_ERROR_TYPE;
}
elsif ( uc($Type) eq "WARN" ) {
$Event{'EventType'} = EVENTLOG_WARNING_TYPE;
}
elsif ( uc($Type) eq "INFO" ) {
$Event{'EventType'} = EVENTLOG_INFORMATION_TYPE;
}
elsif ( uc($Type) eq "AUDIT" ) {
$Event{'EventType'} = EVENTLOG_AUDIT_SUCCESS;
}
else {
$Event{'EventType'} = EVENTLOG_AUDIT_FAILURE;
}my $LogEvent = Win32::EventLog->new("Application")
or die "Can't open Application Event Log to enter $Description\n";
$LogEvent->Report( \%Event ) or die($!);
$LogEvent->Close();Posted 5 years ago # -
You have run into a problem that I write about in my chapter 5 of my second book, Win32 Perl Scripting: The Administrator's Handbook, (http://www.roth.net/books/handbook/). What you are seeing is by design of the Event Log.
When data is stored in an event they are stored as arrays of strings. Also stored in the event is a message ID number and an event message provider. The provider is typically a DLL that contains a message table containing messages each with its own ID.When you create an event you have to identify which provider (message table; this is the "Event Source") to use and supply the message ID (which message within the message table; this is the "Event ID"). These mesasges contain special symbols that are swapped out for the array data you specified when you created the event.Details on how this all works: http://msdn2.microsoft.com/en-us/library/aa363681.aspx
For a Perl script all you need to use is Win32::EventLog::Message. You can download it from our FTP site or install it from our package repository:perl ppm.pl -install http://www.roth.net/perl/packages/win32-eventlog-message.ppdHow it works:
- Your script registers an event log source
- You specify an event ID and supply string data when creating an event.
- Life is good.Note that only machines that have the Win32::EventLog::Message installed and registered will be able to display the nice message text. Otherwise it will come out as you noted.
Posted 5 years ago #
Reply
You must log in to post.