Multiple connections using single sessionOptions

Advertisement

flyingbison
Guest

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?

Reply with quote

Advertisement

flyingbison
Guest

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
        }

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,286
Location:
Prague, Czechia

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
}

Reply with quote

Advertisement

You can post new topics in this forum