Author Posts

September 13, 2018 at 12:22 am

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

 

September 13, 2018 at 1:41 am

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)"

 

 

September 13, 2018 at 12:26 pm

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