Link to home
Start Free TrialLog in
Avatar of Albert Widjaja
Albert WidjajaFlag for Australia

asked on

Modifying PowerShell script to list last 5 Event ID not working ?

Hi All,

I got this script that used to work to report the last 5 error & warning from multiple Exchange Server then write it into Excel.

Somehow this is not working ?

$objExcel = New-Object -comobject Excel.Application
$objExcel.visible = $True
$objWorkbook = $objExcel.Workbooks.Add()
$objSheet = $objWorkbook.Worksheets.Item(1)
$objSheet.Cells.Item(1,1) = "Server"
$objSheet.Cells.Item(1,2) = "LogName"
$objSheet.Cells.Item(1,3) = "Time"
$objSheet.Cells.Item(1,4) = "Source"
$objSheet.Cells.Item(1,5) = "Message"
$objSheetFormat = $objSheet.UsedRange
$objSheetFormat.Interior.ColorIndex = 19
$objSheetFormat.Font.ColorIndex = 11
$objSheetFormat.Font.Bold = $True

$row = 1

$servers = Get-ExchangeServer

foreach ($server in $servers)
{
  $row = $row + 1
  $AppLog = Get-EventLog -LogName Application -computer $server -Newest 5 | Where-Object {$_.EntryType -like 'Error' -or $_.EntryType -like 'Warning'} | Sort-Object Source
  $SecLog = Get-EventLog -LogName Security -computer $server -Newest 5 -ea Silentlycontinue | Where-Object {$_.EntryType -like 'Error' -or $_.EntryType -like 'Warning'} | Sort-Object Source
  $SysLog = Get-EventLog -LogName System -computer $server -Newest 5 | Where-Object {$_.EntryType -like 'Error' -or $_.EntryType -like 'Warning'} | Sort-Object Source
  foreach ($Cat in $AppLog,$Syslog,$Seclog)
  {
    if ($cat -is [array])
    {
      if ($AppLog -contains $cat[0]) {$Catname = "Application"}
      if ($SecLog -contains $cat[0]) {$Catname = "Security"}
      if ($SysLog -contains $cat[0]) {$Catname = "System"}
      Foreach ($event in $cat)
      {
        $objSheet.Cells.Item($row,1).Font.Bold = $True
        $objSheet.Cells.Item($row,1) = $server
        $objSheet.Cells.Item($row,2) = $Catname
        $objSheet.Cells.Item($row,3) = $Event.TimeGenerated
        $objSheet.Cells.Item($row,4) = $Event.Source
        $objSheet.Cells.Item($row,5) = $Event.Message
        $row = $row + 1
      }
    }
  }
}

$objSheetFormat = $objSheet.UsedRange
$objSheetFormat.EntireColumn.AutoFit()
$objSheetFormat.RowHeight = 15

Open in new window


Any help would be greatly appreciated.

Thanks,
Avatar of David Sankovsky
David Sankovsky
Flag of Israel image

I believe that like most scripting languages, variables are case sensitive.
In line 25 you define $Cat as members of $AppLog,$Syslog,$Seclog
but in line 27 you reference $cat (a lowercase c).

Another thing, I'm not entirely certain that you can build an array from 3 result types at the same time (foreach ($Cat in $AppLog,$Syslog,$Seclog))
Avatar of Bill Prew
Bill Prew

Variable names are not case sensitive in Powershell.


»bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Albert Widjaja

ASKER

Many thanks, guys.

THis is working greatly :-)