Hello
I am trying to parse a log file for entries with in the last 24 hours an keep getting an error.
heres is a smaple line form the log (chocolatey) :
2015-10-20 22:29:51,313 [DEBUG] - Attempting to delete file “C:\ProgramData\chocol
atey.chocolatey\teamviewer.10.0.43174.sxs”.
here is the error I am getting:
Exception calling “ParseExact” with “3” argument(s): “String was not recognized
as a valid DateTime.”
here i sthe code I am using.
$FilePath = "C:\ProgramData\chocolatey\logs\chocolatey.log"
$Strings = '[ERROR] -'
$data = Get-Content $filepath # logfile path
write-host '------------------------------------------------------'
write-host '------------------------------------------------------'
write-host Total lines read from file $data.count # printing stats of
write-host '------------------------------------------------------'
$match_string = ''
$IsMatchingRecordFound = ''
#looping throgh all lines
foreach ($line in $data)
{
#fetching date and converting into string format
[DateTime]$date = [DateTime]::ParseExact($line.substring(1, 21), "MMddyyyy-HH:mm:ss:fff", (New-Object System.Globalization.CultureInfo "en-US"))
#comparing date date with start and end
if (($date -gt $StartDate) -and ($date -lt $EndDate))
{
#matching string and array from file data
$match_string = $line | Select-String -Pattern $Strings -SimpleMatch
$match_string
if([string]::IsNullOrEmpty($match_string))
{
}
else
{
$IsMatchingRecordFound="Yes"
}
}
$match_string=''
}
IF([string]::IsNullOrEmpty($IsMatchingRecordFound))
{
write-host 'No Records Found.'
}