Dave,

Thanks for responding.

Yes that file fixed it, I was able to run it on my XP machine now.

One question I have, I am trying to figure out where I can put in the exact
error message?

The way the spreadsheet looks now is, there is a field for Description on
the top, but no data is placed in it.

Also I noticed the Event always says None, when there should be a number.
I am thinking that it is really Category data instead of Event.

I was going to attach the spreadsheet for example, but it got blocked from
the list due to the size.




                                                                                       
                                                       
                    [EMAIL PROTECTED]                                                      
                                                       
                    t                    To:     [EMAIL PROTECTED], 
[EMAIL PROTECTED]                 
                                         cc:                                           
                                                       
                    12/15/2001           Subject:     RE: Help with one of Dave Roth's 
examples                                               
                    04:29 AM                                                           
                                                       
                                                                                       
                                                       
                                                                                       
                                                       



Try downloading the .zip archive again, there has been a modification that
fixes the missing file.
I am attaching the script to this message.

I just ran it on my machine and it completed without errors. It has been
tested on Office 2000 and XP.

dave

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]
Sent: Friday, December 14, 2001 12:35 PM
To: [EMAIL PROTECTED]
Subject: Help with one of Dave Roth's examples


I just picked up the book Win32 Perl Programming: Standard Extensions:
Second Edition, by Dave Roth

It is a superb book, and learning a lot about Perl-Win32.

There was one script I was really interested in and unfortunately I could
not download it for some reason, it was not included in the examples.

So I just copied it myself from the book, took some time but it is worth
it.
The script goes out and grabs all the errors from the event viewers and
puts
the information into a Microsoft Excel spreadsheet.  The script gets as far
as opening Microsoft Excel, but then it closes and the error is below. When
I first ran it I did have some syntax errors, but I fixed those right away.
Now I am stumped I am getting the following error:

"Can't Call method "Worksheets" on an undefined value at eventviewer.pl
line
174"

The exercise is 5.27 in the book, on pages 271-276.

Here is the line it is referring to but you can see the whole script below
in the sub SetupWorksheet:

while( $Sheet->{Parent}->Worksheets( $Name ) )

If anyone can take a look at the script below and if you think you know
what
might be the error.  I would greatly appreciate it.  I am not sure if I
copied the script worng, I double-checked everything, but since it is a
long
script I could have missed something easily, or there was a misprint in the
book.

Thanks:

use Win32::OLE qw ( with );
use win32::OLE::Variant;
use Win32::EventLog;
use Win32::NetAdmin;
use Win32::OLE::Const 'Microsoft Excel';

$Win32::EventLog::GetMessageText = 1;
$DateFormat = "DDD mmm dd, yyyy - hh:mm:ss";
$Class = "Excel.Application";
$Domain = "";


@EVENT_SOURCES = ( "System", "Application");
$EVENT_TYPE = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE;

$Dir = "c:\\temp";
$FileName = "AdminReport.xls";
$File = "$Dir\\$FileName";

$SecPerDay = 24 * 60 * 60;
$Now = time();
$TimeLimit = $Now - ( $SecPerDay * 1 );

$Excel = GetApplication( $Class) || Error( "Could not start $Class" );
$Book
= GetWorkbook( $Excel ) || Error( "Could not obtain a workbook" ); $Sheet =
GetWorksheet( $Book ) || Error( "Could not obtain a worksheet" );


print "Fetching list of servers...\n"; Win32::NetAdmin::GetServers( '' ,
$Domain, SV_TYPE_DOMAIN_CTRL | SV_TYPE_DOMAIN_BAKCTRL, \@Servers );

$Row = 3;

foreach $Machine ( sort (@Servers) )
{
     print "Processing $Machine.\n";
     $Sheet->Range( "A" . ++$Row )->{Value} = $Machine;
     $Sheet->Range( "A$Row" )->{Font}->{Bold}=1;
     map{ ProcessLog( $Machine, $_, $EVENT_TYPE ); } ( @EVENT_SOURCES );
     $Row++;
}
ShutDownSheet( $Sheet );


sub ProcessLog
{
     my ( $Machine, $Source, $Type )=@_;
     my $Flag, $Num, %Hash;
     my $Event = new Win32::EventLog( $Source, $Machine ) ||
          sub
          {
               $Sheet->Range( "B" . ++$Row )->{Value} = "Unable to
connect.";
               return;
          };

if( $Event->GetNumber( $Num) )
{
     my $iCount = 0;
     $Flag = EVENTLOG_BACKWARDS_READ | EVENTLOG_SEQUENTIAL_READ;
     do
     {
          if( $Event->Read( $Flag, $Num, \%Hash ) )
          {
               print "\r $Source records processed: ", ++$iCount, " " x 10;
               if( $Hash{EventType} & $Type )
               {
                    my ( $EventType, $Color, $Time );

                    if( $Hash{EventType} == EVENTLOG_ERROR_TYPE )
                    {
                         $EventType = "Error";
                         $Color = 3;
                    }
                    elsif( $Hash{EventType} == EVENTLOG_WARNING_TYPE )
                    {
                         $EventType = "Warning";
                         $Color = 53;
                    }
                    elsif( $Hash{EventType} == EVENTLOG_INFORMATION_TYPE )
                    {
                         $EventType = "Information";
                         $Color = 1;
                    }
                    $Row++;

                    $Time = "" . localtime( $Hash{TimeGenerated} );
                    $Time = ~ s/^.*?\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*)/$1 $2
$4
$3/;
                    $sheet->Range( "B$Row:H$Row" )->{Value} = [
                      "$Source: $EventType",
                      $Hash{Source},
                      ($Hash{Event}) ? $Hash{Event}:"None",
                      ($Hash{User}) ? $Hash{User}:"N/A",
                       $Hash{Computer},
                      new Win32::OLE::Variant( VT_DATE, $Time ),
                      $Hash{Message}
                    ];
                    $Sheet->Range( "B$Row" )->{Font}->{ColorIndex}
= $Color;
               }
          }



$num=0;
     } while( $TimeLimit < $Hash{TimeGenerated} );
       print "\n";
       Win32::EventLog::CloseEventLog( $Event->{handle} );
     }
}


sub GetApplication
{
     my( $Class ) = @_;
     my( $Application );

     if( ! ( $Appplication = Win32::OLE->GetActiveObject( $Class ) ) )
     {
       $Application = new Win32::OLE( $Class, "Quit" ) || die
     }
     $Application->{Visible} = 1;
     return( $Application );
}


sub GetWorkbook
{
     my( $Application ) = @_;
     my $Book;
     if( ! ( $Book = $Application->Workbooks( $FileName ) ) )
     {
       if( ! ( $Book = $Application->Workbooks->Open( $File ) ) )
       {
          my $Temp = $Application->{SheetsInNewWorkbook};
          $Application->{SheetsInNewWorkbook}=1;

          $Book = $Application->Workbooks->Add();
          $Book->SaveAs( $File );

          $Application->{SheetsInNewWorkbook} = $Temp;
          $UseSheetNumber = 1;
       }
     }
$Book->{Application}->Windows( $Book->{Name} )->{Visible} = 1; return
( $Book
); }



sub GetWorksheet
{
     my( $Book ) = @_;
     my( $Sheet );
     if ($UseSheetNumber )
     {
          $Sheet = $Book->Worksheets( $UseSheetNumber );
     }
     else
     {
          $Sheet = $Book->Worksheets()->Add();
     }
     SetupWorksheet( $Sheet );
     return ( $Sheet );
}

sub SetupWorksheet
{
     my( $Sheet );
     my( $Range );
     my( $Date, $Name, $iCount, @Date );
     @Date = localtime();
     @Date = sprintf( "%04d.%02d.%02d", $Date[5] + 1900, $Date[4] + 1,
$Date[3] );
     $Name = $Date;
     $iCount = 1;



# The line below is the one giving me the error


     while( $Sheet->{Parent}->Worksheets( $Name ) )
     {
        $Name = "$Date #" . ++$iCount;
     }

     $Sheet->{Name} = $Name;
     $Range = $Sheet->Range( "A1" );
     $Range->{Value} = "Server Error Logs for the morning of $Date";
     $Range->{Font}->{Size}=24;
     $Range->{Font}->{ColorIndex} = 6;
     $Sheet->Rows("1:1")->{Interior}->{ColorIndex} =5;
     $Range = $Sheet->Range("A3:H3");
     $Range->{Value} = [
          "Server", "Type", "Source", "Event", "User", "Computer", "Time",
"Description" ];
     with( $Range->{Font},
       Bold => 1,
       Italic => 1,
            Size => 16
     );

     $Range->{HorizontalAlignment} => xlCenter;
     with( $Sheet->Columns( "G" ),
       NumberFormat => $DataFormat,
       HorizontalAlignment => xlCenter
     );
}


sub ShutDownSheet
{

     my( $Sheet ) =0_;
     $Sheet->Columns( "B:G" )->AutoFit();
     $Sheet->Columns( "H:H" )->{ColumnWidth} = 72;
     $Sheet->Rows()->AutoFit();
     $Sheet->{Parent}->Save();
     $Sheet->{Parent}->Close();
}


sub Error
{
     my ( $Error ) = @_;
     print "$Error\n";
     exit();
}






_______________________________________________
Perl-Win32-Admin mailing list [EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin






_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-admin

Reply via email to