Post a reply

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

martin

Re: Multiple connections using single sessionOptions

This looks like a simple PowerShell programming question, not a WinSCP question. Are you looking for this?
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::$Protocol
    HostName = $HostName
    PortNumber = $PortNumber
    UserName = $UserName
}
if($ConnectionType -eq 'SftpSSH')
{
    $sessionOptions.SshHostKeyFingerprint = $SshHostKeyFingerprint
    $sessionOptions.SshPrivateKeyPath =  $SshPrivateKeyPath
    $sessionOptions.PrivateKeyPassphrase = $PrivateKeyPassphrase
}
if($ConnectionType -eq 'Ftp')
{
    $sessionOptions.Password = $Password
}
if($ConnectionType -eq 'Active')
{
    $sessionOptions.FtpMode = [WinSCP.FtpMode]::$FtpMode
}


Though I do not see the point of the "Active" type. It should be merged with the "Ftp" type:
if($ConnectionType -eq 'Ftp')
{
    $sessionOptions.Password = $Password
    $sessionOptions.FtpMode = [WinSCP.FtpMode]::$FtpMode
}
flyingbison

Re: Multiple connections using single sessionOptions

I have multiple connections that I loop through. So for each connection type I have an if statement to determine which to use.
For example:
if($ConnectionType -eq 'SftpSSH')
{
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::$Protocol
        HostName = $HostName
        PortNumber = $PortNumber
        UserName = $UserName
        SshHostKeyFingerprint = $SshHostKeyFingerprint
        SshPrivateKeyPath =  $SshPrivateKeyPath
        PrivateKeyPassphrase = $PrivateKeyPassphrase
        }
}
if($ConnectionType -eq 'Ftp')
{
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::$Protocol
        HostName = $HostName
        PortNumber = $PortNumber
        UserName = $UserName
        Password = $Password
        }
}
if($ConnectionType -eq 'Active')
{
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::$Protocol
        HostName = $HostName
        UserName = $UserName
        Password = $Password
        FtpMode = [WinSCP.FtpMode]::$FtpMode
        }
}

What I would like to do is simplify my code, if possible, by removing the multiple if statements and passing a value to ignore the ones that are not used. I tried $null and "" but that did not work.
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::$Protocol
        HostName = $HostName
        PortNumber = $PortNumber
        UserName = $UserName
        Password = $Password
        SshHostKeyFingerprint = $SshHostKeyFingerprint
        SshPrivateKeyPath =  $SshPrivateKeyPath
        PrivateKeyPassphrase = $PrivateKeyPassphrase
        FtpMode = [WinSCP.FtpMode]::$FtpMode   
        FtpSecure = [WinSCP.FtpSecure]::$FtpSecure
        TlsHostCertificateFingerprint = $TlsHostCertificateFingerprint
        }
martin

Re: Multiple connections using single sessionOptions

I do not understand. Can you show us your code?
flyingbison

Multiple connections using single sessionOptions

Hi
I am currently writing a script to connect to multiple remote servers but some use sftp, sftp with ssh encryption, ftp, ftp with implicit TLS, ftp with explicit TLS, and ftp active. Currently I list out each type of connection with an if statement to determine which to use. Is there a way to to do this with only one sessionOption object rather than six different if statements?