In PowerShell, I am trying to convert a string that I selected from a text file to a date so I can add or substract days, hours and so on from it. The problem is that when I try to convert the resulted string from the text file, I receive the following error:
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At line:15 char:5
+ [datetime]::ParseExact($SC4,'HH:mm:ss', $null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
The Code:
Get-Content results2.txt | ForEach-Object{
$splitUp = $_ -split " "
$SC=$splitUp[0] -split " "
$SC2=$splitUp[1] -split " "
$SC3=$SC + $SC2
Write-Host -NoNewline $SC3 | Write-Output $SC4
Write-Host $SC4
$r=[datetime]::ParseExact($SC4,'dd/MM/yyyy HH:mm:ss', $null)
echo $r
}
The file result2.txt, from which I read, contains the following:
09/07/2017 16:35:56 - 2017-09-07 14:55:51,312 [342]
09/07/2017 16:35:56 - 2017-09-07 14:55:51,312 [342]
09/07/2017 16:35:56 - 2017-09-07 14:56:54,918 [305]
09/07/2017 16:35:56 - 2017-09-07 14:58:36,133 [113]