PowerShell Script Error – "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOpti

Advertisement

alkyred
Joined:
Posts:
4
Location:
USA

PowerShell Script Error – "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOpti

Trying to automate weekly FTP transfers using a powershell script. I am getting the following error:
An error occurred: Exception calling "Open" with "1" argument(s): "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOptions.SshHostKeyFingerprint is not set."
Here is the section of my script that seems to be the issue. It does not like
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
CODE
# Create session options for SFTP
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = $sftpServer
    UserName = $sftpUsername
    Password = $sftpPassword
}
 
# Create session and connect
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
 
# Retrieve and set SSH host key fingerprint
$sshHostKeyFingerprint = $session.SessionOptions.HostKey.ToString()
$sessionOptions.SshHostKeyFingerprint = $sshHostKeyFingerprint
 
# Set transfer options to download only zip files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*.zip"
I appreciate any help.
Thanks

Reply with quote

Advertisement

alkyred
Joined:
Posts:
4
Location:
USA

Re: PowerShell Script Error – "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOpti

Thank you martin! I am closer. Now I am getting the following:
An error occurred: Exception calling "Open" with "1" argument(s): "Connection has been unexpectedly closed. Server sent command exit status 0.
Authentication log (see session log for details):
I wonder if it is because there is a banner when connecting to the site and its timing out because I cannot see the banner, therefore I cannot agree.

Reply with quote

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

Re: PowerShell Script Error – "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOpti

The banner should not be a problem. Please attach a full session log file showing the problem (using the latest version of WinSCP). If you can connect anyhow (like in the GUI), please post log for that too.

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

alkyred
Joined:
Posts:
4
Location:
USA

Re: PowerShell Script Error – "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOpti

I added the following to try and generate a log file from the PowerShell script:
# Create session and connect
$session.SessionLogPath = "C:\temp\FTP.log"
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
But the log file is not populating.
From the GUI I get this in the log file that is attached.
Description: GUI Log

Reply with quote

Advertisement

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

Re: PowerShell Script Error – "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOpti

Well, you have to first create the object and only after that you can assign its properties.
$session = New-Object WinSCP.Session
$session.SessionLogPath = "C:\temp\FTP.log"

Reply with quote

alkyred
Joined:
Posts:
4
Location:
USA

Re: PowerShell Script Error – "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOpti

I had the code in the session options section. I still do not get any logging. here is the updated code.
# Create session and connect
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
$session.SessionLogPath = "C:\temp\FTP.log"
Todd

Reply with quote

martin
Site Admin
martin avatar

Re: PowerShell Script Error – "SessionOptions.Protocol is Protocol.Sftp or Protocol.Scp, but SessionOpti

You have to set Session.SessionLogPath before opening the connection – before calling Session.Open. You current code throws an exception – haven't you noticed? If you haven't you probably have some bad error reporting in place.

Reply with quote

Advertisement

You can post new topics in this forum