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

Welcome Forums General PowerShell Q&A I only want the first event based on source...

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
9 months ago.

  • Author
    Posts
  • #103375

    Participant
    Points: 24
    Rank: Member

    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

    Keymaster
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Add "-first 1" to Select-Object?

  • #103396

    Participant
    Points: 24
    Rank: Member

    That's it, excellent!
    Thanks Don.

The topic ‘I only want the first event based on source...’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort