0

I have a script that we use to connect to various SFTP servers, either by using username/password or SSH key, when using SSH key we normally use a passphrase, but in this case the key is not passphrase protected, I haven't been able to establish a connection due to an error related to the lack of passphrase, an excerpt of the script below:

# Setup variables
$remotePath = "${FTPRemotePath}"
$wait_time_seconds = ${wait_time_seconds}

# Hardcoded variables
$output_code = 0
$FilePresent = $false

# Check if using SSH key or username/password
if (![string]::IsNullOrEmpty("${FTPPrivateKeyPath}")){
    # Set up session options for SSH key
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "${FTPHostName}"
        PortNumber = "${FTPPortNumber}"
        UserName = "${FTPUserName}"
        SshHostKeyFingerprint = "${FTPFingerprint}"
        SshPrivateKeyPath = "${FTPPrivateKeyPath}"
        PrivateKeyPassphrase = ""
        Timeout = 300
    }
    Write-Host "Authentication with SSH Key"
}else{
    # Set up session options for username/password
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "${FTPHostName}"
        PortNumber = "${FTPPortNumber}"
        UserName = "${FTPUserName}"
        SshHostKeyFingerprint = "${FTPFingerprint}"
        Password = "${FTPPassword}"
        Timeout = 300
    }
    Write-Host "Authentication with username/password"
}

Write-Host "Waiting for file pressence on SFTP server. Expected file: $($remotePath)"

try
{
    # Format timestamp
    $stamp = $(Get-Date -Format "yyyyMMdd")
    
    # Check if file present, if not wait for x seconds and recheck
    While (!$FilePresent){
        # Connect
        $session = New-Object WinSCP.Session
        $session.Timeout = New-TimeSpan -Seconds 300
        $session.SessionLogPath = "${WinSCP_LOG_DIR}\${FTPUserName}_winscp_check_$stamp.log"
        $session.Open($sessionOptions)
        
        if ($session.FileExists($remotePath)){
            Write-Host "File found on SFTP server"
            $FilePresent = $true
            $session.Dispose()
            $output_code = 1992
        }else{
            $session.Dispose()
            Start-Sleep -s $wait_time_seconds
        }
    }
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    $session.Dispose()

Regardless of setting an empty PrivateKeyPassphrase variable in the session options or not setting it at all I am always getting the following error:

-7331483.PS1:15 char:23
+     $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [New-Object], Exception
    + FullyQualifiedErrorId : SetValueException,Microsoft.PowerShell.Commands. 
   NewObjectCommand
 
Authentication with SSH Key
Waiting for file pressence on SFTP server. Expected file: download/RXXXX_NN.ZIP
Error: Exception calling "Open" with "1" argument(s): "Value cannot be null.
Parameter name: sessionOptions"

Any suggestions on how to modify this to be able to connect with an ssh key but no passphrase? This same script is working for all other connections I have.

Thanks in advance

Faizers
  • 38
  • 6
  • It's a `New-Object WinSCP.SessionOptions` call failure that's the root cause of your problem. I believe the output you have posted excludes the most important part. One of the `SessionOptions` properties your are setting is invalid. And it's mostly likely not the `PrivateKeyPassphrase`. I believe your conclusions are not correct. See [WinSCP .NET assembly in PowerShell - Creating SessionOptions - The value supplied is not valid, or the property is read-only](https://stackoverflow.com/q/43596109/850848). If that does not help, we need [mcve]. – Martin Prikryl Mar 21 '23 at 15:32

0 Answers0