locked
not able to display array output in color code. RRS feed

  • Question

  • This is small script to get events after last reboot and color code the output depending on it’s error event or not

    Function getlastreboottime{
    (get-eventlog -LogName System | where {$_.EventID -eq ‘1074’} | select -First 1).TimeGenerated
    }

    Function GetEventsAfterLastReboot($time){
    get-eventlog -LogName System -After $time
    }

    $LastRebootime = getlastreboottime
    Write-host The system was last reboot on $LastRebootime -ForegroundColor Green
    $events = GetEventsAfterLastReboot($LastRebootime)
    Write-Host Following events after last reboot. The error evens are marked in RED -ForegroundColor Green

    foreach ($event in $events){
    if ($event.entrytype -eq ‘Error’){
    write-host $event -ForegroundColor red
    }
    else {(write-host $a -ForegroundColor Green)
    }
    }

    But this is the output.. It is not displaying the the event details

    please help me.

    The system was last reboot on 13-05-2020 18:20:31
    Following events after last reboot. The error evens are marked in RED
    System.Diagnostics.EventLogEntry
    System.Diagnostics.EventLogEntry
    System.Diagnostics.EventLogEntry

    Friday, May 15, 2020 9:41 AM

All replies

  • Please do not post duplicate requests in this forum.  It will not get you an answer any faster and is not helpful to others looking for similar solutions.

    I recommend that you take some time to learn PowerShell.   Also note that the CmdLet, "Get-Eventlog", has been deprecated since Windows Vista.  YOu need to use "Get-WinEvent"

    Arrays in PowerShell must be enumerated,.  To learn how to do this and how it is a fundamental method in comoputers and cosing in all languages you will need to learn PowerShell.

    Learning to script properly with PowerShell


    \_(ツ)_/

    Friday, May 15, 2020 10:29 AM
  • Here is a quick demo of how to access these events.  You will have to learn  enough to figure out what your output should look like.

    $timeCreated = Get-WinEvent -FilterHashTable @{Logname='System';ID=1074} -MaxEvents 1 | select -expand TimeCreated
    Write-host The system was last reboot on $timeCreated -ForegroundColor Green
    
    Write-Host Following events after last reboot. The error evens are marked in RED -ForegroundColor Green
    Get-WinEvent -FilterHashTable @{Logname='System';StartTime=$timeCreated} |
        ForEach-Object{
            if($_.LevelDisplayName -eq 'Error'){
                write-host $_.Message -ForegroundColor red
            }else{
                write-host $_.Message -ForegroundColor Green
            }
        }


    \_(ツ)_/


    Friday, May 15, 2020 10:46 AM