Although I was able to achieve my objective, I don't understand why and I am concerned I'm still doing something wrong.
The context is to retrieve a datetime value for comparison to another date using FTP to retrieve a file's timestamp. The datetime is received as a string using this:
$FTPrequest = [System.Net.FtpWebRequest]::Create($FTPTargetFile)
$FTPrequest.Method = [System.Net.WebRequestMethods+FTP]::GetDateTimestamp
$FTPrequest.Credentials = $Credentials
$response = $FTPrequest.GetResponse().StatusDescription
$tokens = $response.Split(" ")
if ($tokens[0] -eq 213) {
$Timestampdata = $tokens[1]
} else {
Write-Output "FTP timestamp request for " + $FTPTargetFile + " returned an error"
}
Almost every available resource I found made it clear that ParseExact uses a string to derive a datetime as in the following line of script:
$d = [DateTime]::ParseExact($Timestampdata,"yyyyMMddHHmmss",$null)
But whether in a script or at the command prompt, the line above consistently returns the following error:
Exception calling "ParseExact" with "3" argument(s): "String was not recognized
as a valid DateTime."
At C:\hw-sw\powershell\FTPupload option - getFileInfo.ps1:38 char:1
+ $d = [DateTime]::ParseExact($Timestampdata,"yyyyMMddHHmmss",$null)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
There were 2 links I found (lin1, link2 - 2nd link addresses a slighty different problem but the point is still made) that suggested ParseExact() uses an intXX instead, as in the following line of script:
$d = [DateTime]::ParseExact([convert]::ToInt64($Timestampdata),"yyyyMMddHHmmss",$null)
And yes, the line above runs without error.
While I am grateful for having found a potential solution to my scripting problem, I am unsettled because I don't understand why ParseExact is working with an int64, and not a string as I would have expected.
Is there some setup in PowerShell that changes how it works? I believe I must be making some simple mistake, but I can't figure it out.
$d = [datetime]::parseexact($Timestampdata.Trim(),"yyyyMMddHHmmss",$null).