Hello fellow redditors and sorry if this is not the right place. I appreciate this may be better suited for the WINSCP forums however someone here may be able to tell me what I am doing wrong.
I am trying to create a function that when called will list the objects in a remote directory. I am basing it off a function i created that downloads files but instead of calling the GetFiles method I am just calling the ListDirectory method.
The issue is when i run this function (with the variables already containing the relevant Ftp server hostname, UN/PW, SSHhostkeyfingerprint and FTP Directory) it doesn’t error out but nothing happens
Function SFTPSessionOpen {
param
(
[string]$FtpServer,
[string]$FtpDirectory,
[string]$FtpUsername,
[string]$FtpPassword,
[string]$FtpSshHostKeyFingerprint
)
## Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("C:Program Files (x86)WinSCPWinSCPnet.dll") | Out-Null
## Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = $FtpServer
$sessionOptions.UserName = $FtpUsername
$sessionOptions.Password = $FtpPassword
$sessionOptions.SshHostKeyFingerprint = $FtpSshHostKeyFingerprint
$session = New-Object WinSCP.Session
$mycollection = @()
try {
## Connect
$session.Open($sessionOptions)
$mycollection = $session.ListDirectory($FtpDirectory).Files
}
finally {
$session.Dispose()
}
}
I would have expected it to pull the directory list into the $mycollection variable when called but it remains empty. If i run part of the function individually (establish a session then use $session.ListDirectory it will list files)
Any help is appreciated!
##Prod
$FtpServer = "clientftp.rimbees.com"
$FtpUsername = "Username"
$Ftppassword = "Password"
$FtpSshHostKeyFingerprint = "ssh-rsa 2048 Wi2ZFNQvI6xvklKznGlZ1Cv5vGB3tV0dtAPH6TFMrJk"
$FtpDirectory = "./"
$mycollection = SFTPSessionOpen -FtpServer $FtpServer -FtpUsername $FtpUsername -FtpPassword $Ftppassword -FtpSshHostKeyFingerprint $FtpSshHostKeyFingerprint -FtpDirectory $FtpDirectory
I call the function with the pre defined variables and expect it to at least establish a session. The end goal is that i run it and it display all the files on the remote directory (in this case root folder)
It works when i run the section in separate blocks however as a function ( i tried using a breakpoint and debugging) it seems to never establish the connection/open the session.