Author Posts

February 23, 2018 at 8:08 pm

I'm using WinSCP PowerShell module from PS Gallery to download file from SFTP Server. I need to throw an error if the file in remote SFTP server is older than 48 hours .

Currently I'm succesfully reading the lastwritetime from remote file and I can get it in format '02/23/2018 01:10:44'. Now I need to compare it to current time and throw an error if the file is older than 48 hours. My local time format is a little different ( 00:00:00) than what's in remote server. Does it matter and do I need to convert them to same format before comparing? Any help would be appreciated.

February 23, 2018 at 8:22 pm

Yeah, casting it as a [datetime] – which is what you would need to do – may break because of your local time format settings. You may end up having to manually parse the remote date into a string based on your local format settings, and then [datetime] cast it.

February 24, 2018 at 3:15 am

[datetime]::ParseExact('22.02.2018 22:03:30','dd.MM.yyyy HH:mm:ss',[cultureinfo]::InvariantCulture)

If the format is consistent, then you can parse it out with ParseExact.

February 24, 2018 at 4:15 pm

If you could use powershell, it would just be (older than 2 days):

$a = ls file                                                                                                                   
if ( (get-date) - $a.LastWriteTime -gt 2. ) { 'do whatever' }       

March 12, 2018 at 6:23 am

OK I learned something new. Date format is coming in correct format from remote server. Problem was that I was using " Write-output "File modified: $($remoteFile.LastWriteTime)" and this causes date format to change in '02/23/2018 01:10:44' format.