Hi All,
Im trying to send two files with winscp to an sftp server. I tried so many things. Im lost.
It need to find the files created in the last 8 hours and then send them to the sftp. I only can do one file.
try { # Load WinSCP .NET assembly Add-Type -Path 'C:\Program Files (x86)\WinSCP\WinSCPnet.dll' # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "*************" UserName = "*************" Password = "*********" SshHostKeyFingerprint = "ssh-rsa 2048 ******" } $session = New-Object WinSCP.Session if (-not (get-psdrive K -ErrorAction SilentlyContinue)) { $pass="********"|ConvertTo-SecureString -AsPlainText -Force $Cred = New-Object System.Management.Automation.PsCredential('********\*******',$pass) New-PSDrive -name K -Root \\********\test -Credential $cred -PSProvider filesystem -Persist } try { # Connect $session.Open($sessionOptions) $localPath = "K:\" $remotePath = "/incoming/*********/" # Select the most recent file. # The !$_.PsIsContainer test excludes subdirectories. # With PowerShell 3.0, you can replace this with -File switch of Get-ChildItem. $latest = Get-ChildItem -Path $localPath | Where-Object {$_.CreationTime -gt $(get-date).addhours(-8)} | Sort-Object LastWriteTime -Descending | Select-Object -First 1 # Any file at all? if ($latest -eq $Null) { Write-Host "No file found" exit 1 } # Upload the selected file $sourcePath = Join-Path $localPath $latest.Name $session.PutFiles( [WinSCP.RemotePath]::EscapeFileMask($sourcePath), [WinSCP.RemotePath]::Combine($remotePath, "*")).Check() } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch { Write-Host "Error: $($_.Exception.Message)" exit 1 }