Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

Guest

Perfect! Thanks!
Guest

$WinSCPSession = New-WinSCPSession -SessionOption (New-WinSCPSessionOption -HostName $sftphost -Protocol Sftp -PortNumber $port -Credential $Cred -SshHostKeyFingerprint $sftpfingerprint)
 
$transferResult = $WinSCPSession.GetFilesToDirectory($sftpDownloadDir, $localDir, '', $deleteRemote)
 
foreach ($fileTransfer in $transferResult.Transfers)
{
    $isSuccess = $fileTransfer.IsSuccess
    $fileName = $fileTransfer.FileName
    # put here inserts to DB like Invoke-Sqlcmd -ServerInstance $sqlServer -Database $database -Query "INSERT INTO YourTable (IsSuccess, FileName) VALUES ('$isSuccess', '$fileName')"
}
 
$WinSCPSession.Close()
$WinSCPSession.Dispose()
jhw4836

File Name when using GetFilesToDirectory

I'm working on a PowerShell script to copy files from an SFTP location to a local machine. Everything is working fine, except for some data I want to obtain once the file(s) are transferred.

When I execute the script I see that it is returning an object showing IsSuccess and FileName. I want to be able to take the returned information and then insert it to a SQL table for later use by another application. I'm able to access the value for IsSuccess but not for FileName (see attachment for output).

I'm not sure if I'm missing something or this feature doesn't exist?

Code for transfer:
#Create WinSCP session
$WinSCPSession = New-WinSCPSession -SessionOption (New-WinSCPSessionOption -HostName $sftphost -Protocol Sftp -PortNumber $port -Credential $Cred -SshHostKeyFingerprint $sftpfingerprint)
 
#Transfer files to local directory
$WinSCPSession.GetFilesToDirectory($sftpDownloadDir,$localDir,'',$deleteRemote)
#return $filename
$WinSCPSession.close()
$WinSCPSession.Dispose()