I only want the first event based on source...

This topic contains 2 replies, has 2 voices, and was last updated by  Martin 3 weeks, 1 day ago.

  • Author
    Posts
  • #103375

    Martin
    Participant

    Hi all,
    I am hoping this is going to be a simple fix but I have been racking my brains with this simple query all day.
    I am trying to make a list of a single entry per remote host of just one result of the most recent event based on a particular source.
    Basically I am using get-eventlog within a script block as using get-eventlog within a loop is far too slow from a source machine.
    Anyway, I have the script working to my liking now except I cannot seem to list ony the last event that happened, rather than all events in the log, does anyone know how to do this, I played around with '-newest 1' but when using this my sript did not return anything at all 🙁

    Please find the script below:

    $FinalList = @()
    $CurrentSystem = @()
    $Machine = get-content servers.txt
    foreach ($Server in $Machine)
    {
    Write-Host $Server
    $CurrentSystem = Invoke-Command -computername $Server -ScriptBlock{
    get-eventlog -logname system | where-object {$_.Source -eq 'pvscsi'} | select-object machinename,source,eventid,message,timegenerated
    }
    write-host $CurrentSystem
    $FinalList += $CurrentSystem
    }
    $FinalList | export-csv ServersWithPvsciEvents.csv
    

    Many thanks.

  • #103391

    Don Jones
    Keymaster

    Add "-first 1" to Select-Object?

  • #103396

    Martin
    Participant

    That's it, excellent!
    Thanks Don.

You must be logged in to reply to this topic.