PowerShell script not downloading data but creates folder when in Task Scheduler
I am trying to have files downloaded daily from SFTP to local and have it create a folder first then add the files to that folder. It works when not in Task Scheduler but task scheduler only creates the folder. Here is the code that I am using below.
$date = ((Get-Date).AddDays(-1)).ToString("MMddyyyy") New-Item -Path "C:\Data" -Name $date -ItemType Directory # Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Set up session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = ********* UserName = ********* Password = ********* SshHostKeyFingerprint = ********** } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Transfer files $remotePath = "C:\Data\$date\*" $session.GetFiles("/datafeed/*", $remotePath, $True).Check() } finally { }