File Name when using GetFilesToDirectory

Advertisement

jhw4836
Joined:
Posts:
1
Location:
US

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()

Screenshot 2023-10-23 at 5.03.41 PM.png

Reply with quote

Advertisement

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()

Reply with quote

Advertisement

You can post new topics in this forum