This topic contains 2 replies, has 2 voices, and was last updated by
September 13, 2018 at 12:22 am #111688ParticipantPoints: 0Rank: 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
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.
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 #111694ParticipantPoints: 0Rank: 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)"
September 13, 2018 at 12:26 pm #111713ParticipantPoints: 292Rank: 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.