When i'm trying to parse a line in a IIS log file and pulling the datetime on the line, I'm getting the following error
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime." At line:9 char:1
- $dateTime = [datetime]::ParseExact($dateTimeString, 'yyyy-MM-dd HH:mm ...
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FormatException
here is my powershell code:
$line = '2020-12-23 02:01:16.244 -06:00 [Information] 2020-12-23T08:01:16.2446161Z: [Error] Oracle.ManagedDataAccess.Client.OracleException (0x80004005): Connection request timed out'
$dateTimeString = [regex]::Matches($line, '\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d')[0].Groups[1].Value
write-host "datetimestr:" $dateTimeString
$provider = New-Object System.Globalization.CultureInfo "en-US"
$dateTime = [datetime]::ParseExact($dateTimeString, 'yyyy-MM-dd HH:mm:ss', $provider)
write-host $dateTime
$dateTime -f 'MM/dd/yyyy HH:mm:ss' | write-host
i guess i need some fresh eyes to see what i'm missing.
notes:
- i've tried copying and pasting the string in the $line var to validate there are no strange chars in the datetime
- i have tried adding .trim to the $line and that didn't help
- i've also narrowed the regex pattern to '\d\d\d-\d\d-\d\d' and parseexact() to ($dateTimeString, 'yyyy-MM-dd', $provider) and this doesn't help either.