Link to home
Start Free TrialLog in
Avatar of bibi92
bibi92Flag for France

asked on

get errors on log

Hello,
dly_test.log contains :
21/05/2021 00:56:56 ERR1014 Une erreur s'est produite.
01/06/2021 00:22:10 ERR1016 Une interruption s'est produite.
04/06/2021 11:40:15 ERR1012 Le planificateur a été stoppé


I want to get errors during $LDays = 3  :

$Time  = (Get-Date).AddDays(-$LDays).ToString('dd/MM/yyyy hh:mm:ss')

LogPath = "C:\TEMP\Log"  
$Log = Get-ChildItem -path $LogPath -recurse -include dly_test.log
$ListError = 'ERR1014','ERR1016','ERR1012','ERR1011','ERR1013'

$Error = Get-Content -Path $Log|
Where-Object { $_ -contains $ListError -and ($_ -split ' ')[0] -gt $Time }

Thank you

Regards
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try this:

$LDays = 3
$Time  = (Get-Date).AddDays(-$LDays)

$LogPath = "C:\TEMP\Log"  
$Log = Get-ChildItem -path $LogPath -recurse -include dly_test.log

$ListError = 'ERR1014','ERR1016','ERR1012','ERR1011','ERR1013'

$Errorz = Get-Content -Path $Log |
Where-Object { $_ -match $ListError -join "|" -and [datetime]::ParseExact((($_ -split ' ')[0] + " " + ($_ -split ' ')[1]), "dd/MM/yyyy hh:mm:ss", $null) -gt $Time }

Open in new window

Avatar of bibi92

ASKER

Hello,
Below error is returned :
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:2 char:16
+ ... re-Object { $_ -match $ListError  -join " " -and [datetime]::Parse ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

Thank you

regards 
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime." 

does your error log contains over details that particularly not starting with a log date time? the script provided try to parse the date time for the first 2 array items delimited with space.
Avatar of bibi92

ASKER

No the error log not contains details not starting with a log date time. But when I try without -join "|" -and [datetime]::ParseExact((($_ -split ' ')[0] + " " + ($_ -split ' ')[1]), "dd/MM/yyyy hh:mm:ss", $null) -gt $Time
$Errorz  is null so maybe a problem with the list.
Thank you 
can you share the content of line 2 ?
Avatar of bibi92

ASKER

Hello,
$LDays = 3
$Time  = (Get-Date).AddDays(-$LDays)
$LogPath = "C:\TEMP\Log"  
$Log = Get-ChildItem -path $LogPath -recurse -include dly_test.log 
$ListError = 'ERR1014','ERR1016','ERR1012','ERR1011','ERR1013'
$Errorz = Get-Content -Path $Log |
Where-Object { $_ -match $ListError -join "|" -and [datetime]::ParseExact((($_ -split ' ')[0] + " " + ($_ -split ' ')[1]), "dd/MM/yyyy hh:mm:ss", $null) -gt $Time }

Open in new window

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:2 char:16
+ ... re-Object { $_ -match $ListError -join "|" -and [datetime]::ParseExac ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:2 char:16
+ ... re-Object { $_ -match $ListError -join "|" -and [datetime]::ParseExac ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException
Thank you 
sorry, I mean the line 2 of file : dly_test.log
Avatar of bibi92

ASKER

04/06/2021 00:56:56 ERR1013 Une interruption s'est produite

Thank you

Best regards 
found the issue on 24 hours format, so try use this instead:

$LDays = 3
$Time  = (Get-Date).AddDays(-$LDays)
$LogPath = "C:\TEMP\Log"  
$Log = Get-ChildItem -path $LogPath -recurse -include dly_test.log
$ListError = 'ERR1014','ERR1016','ERR1012','ERR1011','ERR1013'
$Errorz = Get-Content -Path $Log |
Where-Object { $_ -match $ListError -join "|" -and [datetime]::ParseExact((($_ -split ' ')[0] + " " + ($_ -split ' ')[1]), "dd/MM/yyyy HH:mm:ss", $null) -gt $Time }

Open in new window


Avatar of bibi92

ASKER

Hello

Same error

Thank you
Avatar of bibi92

ASKER

The problem is on $ListError, If I change $_ -match 'ERR1013'  
$LDays = 3
$Time  = (Get-Date).AddDays(-$LDays)
$LogPath = "C:\TEMP\Log"  
$Log = Get-ChildItem -path $LogPath -recurse -include dly_test.log
$ListError = 'ERR1014','ERR1016','ERR1012','ERR1011','ERR1013'
$Errorz = Get-Content -Path $Log |
Where-Object { $_ -match 'ERR1013' -and [datetime]::ParseExact((($_ -split ' ')[0] + " " + ($_ -split ' ')[1]), "dd/MM/yyyy HH:mm:ss", $null) -gt $Time }

Open in new window


The result is returned regardless of the date but I don't see what I can modify on Where-Object  :
04/06/2021 00:56:56 ERR1013 Une interruption s'est produite
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bibi92

ASKER

No I confirm it works I reset powershell session
Thank you for your support