Here's my code
# $index is 12 (for exif 'date taken')
$value = $dir.GetDetailsof($file, $index)
$value = $value.Trim()
if ($value -and $value -ne '') {
return [DateTime]::ParseExact($value, "g", $null)
}
return $null
this always throws the following exception
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At REDACTEDPATH.ps1:64 char:16
+ return [DateTime]::ParseExact($value, "g", $null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
If I use return [DateTime]::ParseExact("09/11/2019 19:16", "g", $null)
the code works as expected
If I go back to using the variable and inspect $value at a breakpoint on the return
line, it looks like 09/11/2019 19:16
If I look at the exif data of the file via the Windows property dialogue, the datetime looks like 09/11/2019 19:16
If I change the code to return [DateTime]::ParseExact($value, "dd\MM\yyyy HH:mm", $null)
it throws the same exception, but if I try return [DateTime]::ParseExact("09/11/2019 19:16", "dd\MM\yyyy HH:mm", $null)
it works. So it seems as though what I'm picking up in $value isn't what I think it is, but I can't see where it differs from my expectation.
Even inserting Write-Host "-$value-"
just after the trim line shows exactly what I'd expect to see: -09/11/2019 19:16-
EDIT here's an image of variable inspection at the breakpoint