locked
Error formating date? RRS feed

  • Question

  • Hi All,

          I'm using Posh-SSH to download a file through SFTP. When I try to format the date to get the correct file string to download it gives me an error. Can't figure out why, please help?

    $LocalPath="d:\test1\"


    $LocalTime=(Get-Date(Get-Date-FormatyyyyMMdd)).AddDays(-1)

    $RemoteFile="POD_EXTRACT_"+$LocalTime.ToString()

    Write-Host$RemoteFile


    Get-Date : Cannot bind parameter 'Date'. Cannot convert value "20170821" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

    At D:\Test1\VSoft_POD.ps1:6 char:25

    +  $LocalTime = (Get-Date (Get-Date -Format yyyyMMdd)).AddDays(-1)

    +                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException

        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand


    POD_EXTRACT_20170821

    PS C:\Windows\System32\WindowsPowerShell\v1.0> 

    Monday, August 21, 2017 4:54 PM

Answers

  • Please do not post colorized code.  Use the code posting tool.

    $LocalTime = [datetime]::Today.AddDays(-1)
    $RemoteFile = "POD_EXTRACT_" + $LocalTime.ToString('yyyyMMdd')


    \_(ツ)_/


    • Edited by jrv Monday, August 21, 2017 5:05 PM
    • Marked as answer by SSIS2DTSConfigFile Monday, August 21, 2017 5:23 PM
    Monday, August 21, 2017 5:04 PM

All replies

  • You have to take a close look but the last two lines are the regular powershell output. It's putting out the correct format but not AddDays(-1). I should be getting the 20th and I'm getting the 21st. Please help.

    Regards,

    Bryan

    Monday, August 21, 2017 5:01 PM
  • Please do not post colorized code.  Use the code posting tool.

    $LocalTime = [datetime]::Today.AddDays(-1)
    $RemoteFile = "POD_EXTRACT_" + $LocalTime.ToString('yyyyMMdd')


    \_(ツ)_/


    • Edited by jrv Monday, August 21, 2017 5:05 PM
    • Marked as answer by SSIS2DTSConfigFile Monday, August 21, 2017 5:23 PM
    Monday, August 21, 2017 5:04 PM