Reputation: 2255
The get-winevent start and end dates are not filtering records. Can anyone tell me why? I expect from the code below the last 2 days events but i gets dates going back to 2010 (my Windows clock date is correct)
[String]$ComputerName = $env:COMPUTERNAME#Current computer
[String[]]$EventLogNames=@("Application","System")#Main eventlogs
[System.DateTime[]]$EventStartDate = (((Get-Date).addDays(-2)).date)#date 10 days ago
[System.DateTime[]]$EventEndTime = (Get-Date)
$EventCritea = @{logname = $EventLogNames; StartTime=$EventStartDate; EndTime=$EventEndTime}
Get-WinEvent -ComputerName $ComputerName -FilterHashTable $EventCritea -ErrorAction SilentlyContinue
Upvotes: 2
Views: 6726
Reputation: 18082
The error in your script is the type of your start and end date/time - they are declared as arrays.
Change
[System.DateTime[]]$EventStartDate = (((Get-Date).addDays(-2)).date)
[System.DateTime[]]$EventEndTime = (Get-Date)
to
[System.DateTime]$EventStartDate = (((Get-Date).addDays(-2)).date)
[System.DateTime]$EventEndTime = (Get-Date)
or omit them altogether
$EventStartDate = (((Get-Date).addDays(-2)).date)
$EventEndTime = (Get-Date)
Upvotes: 6