Home > Software engineering >  WinSCP Power shell script is generating error "The value supplied is not valid, or the property
WinSCP Power shell script is generating error "The value supplied is not valid, or the property

Time:02-25

I my trying to connect to a client SFTP server from our Windows Server to pull the files. The connection works fine when trying from my Local machine both using WinSCP application and powershell script. But I have to add proxy setting while I am trying to connect from the server which is not the case in Local connection. While in the server I am able to connect from WinSCP application by enabling proxy setting but fails when I am trying to connect using powershell script. I got the custom generated powershell script from WinSCP connection. Below is the code snippet and corresponding error message.

**

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::SFTP
        HostName = "<host name>"
        PortNumber = 22
        UserName = "<Username>"
        SshPrivateKeyPath= "<private key>"  
        SshHostKeyFingerprint = "<SshHostKeyFingerprint>"
    }
    
    $sessionOptions.AddRawSettings("ProxyMethod", "3")
    $sessionOptions.AddRawSettings("ProxyHost", "<proxyhost>")
    $sessionOptions.AddRawSettings("ProxyPort", "3128")
    $sessionOptions.AddRawSettings("ProxyLocalhost", "1")

**

WinSCP generated custom code

getting below error message.

New-Object : The value supplied is not valid, or the property is read-only. Change the value, and then try again.
At line:3 char:19
  $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (:) [New-Object], Exception
      FullyQualifiedErrorId : SetValueException,Microsoft.PowerShell.Commands.NewObjectCommand
 
You cannot call a method on a null-valued expression.
At line:12 char:1
  $sessionOptions.AddRawSettings("ProxyMethod", "3")
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:13 char:1
  $sessionOptions.AddRawSettings("ProxyHost", "<proxyhost>")
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:14 char:1
  $sessionOptions.AddRawSettings("ProxyPort", "3128")
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:15 char:1
  $sessionOptions.AddRawSettings("ProxyLocalhost", "1")
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      FullyQualifiedErrorId : InvokeMethodOnNull

CodePudding user response:

The most probable explanation is that the value of SessionOptions.SshHostKeyFingerprint is invalid, no other property in your code is validated.

See also WinSCP .NET assembly in PowerShell - Creating SessionOptions - The value supplied is not valid, or the property is read-only

As your fingerprint format seems correct, but as it is a "modern" ssh-ed25519 key, I assume that you have an old version of WinSCP .NET assembly that does not accept this key. You have probably used newer version of WinSCP to obtain this key, but your version of .NET assembly does not accept it. Make sure you use the latest version of the assembly.

  • Related