Date Log Question

Welcome Forums General PowerShell Q&A Date Log Question

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

 
Participant
3 months ago.

  • Author
    Posts
  • #111688

    Participant
    Points: 0
    Rank: Member

    I was looking at getting error event logs from the previous day, when looking at the different post on the forums I come across different day/time PowerShell formats that range from a specific date.  I was looking at this PowerShell format for a date
    Example 11: Get all errors in an event log that occurred during a specific time frame

    PowerShellCopy
     

    PS C:\> $May31 = Get-Date 5/31/08
    PS C:\> $July1 = Get-Date 7/01/08
    PS C:\> Get-EventLog -Log "Windows PowerShell" -EntryType Error -After $May31 -before $July1

    I was wondering what would be the correct way to have this PowerShell search for errors from the previous day, so if its run on Tuesday it will gather all Mondays, Friday will gather all Thursdays.  This is how I started doing it with no success.
    PowerShellCopy

    PS C:Get-EventLog -LogName System -EntryType Error, Warning; starttime=[datetime]::today
    PS C:Get-EventLog -LogName Application -EntryType Error, Warning; startime=[datetime]::today
    PS C:Get-EventLog -LogName Security -EntryType Error, Warning; starttime=[datetime]::today

     

  • #111694

    Participant
    Points: 0
    Rank: Member

    I found a way around this for the previous day, I went with a different easier for me format.
    "Get-EventLog -Log "System" -EntryType Error, Warning -After (get-date).AddDays(-1)"

     

     

  • #111713

    Participant
    Points: 292
    Helping Hand
    Rank: Contributor

    You definitely have the right idea here.

    You may want to do it slightly differently if you want "the previous day" as opposed to "the previous 24 hours precisely" (which would be from the current time to the same time the previous day):

    $Today = (Get-Date).Date # Removes the hour/minute/second and anchors on the start of the day
    $Yesterday = $Today.AddDays(-1)
    Get-EventLog -Log System -EntryType Error, Warning -Before $Today -After $Yesterday

The topic ‘Date Log Question’ is closed to new replies.