Connect to SFTP that uses passphrase with PowerShell script

Advertisement

Kell
Guest

Connect to SFTP that uses passphrase with PowerShell script

Hello

I'm trying to use the following code to connect to SFTP with a passphrase. It's for a PowerShell script.
New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = $HostName
        UserName = $User
        PortNumber = $Port
        SshHostKeyFingerprint = $HostKey
        SshPrivateKeyPath = $Password
        SecurePrivateKeyPassphrase= $Passphrase
}
However I'm getting the following message:
Exception calling "Open" with "1" argument(s): "Network error:
Connection to "st1.primetherapeutics.com" timed out."
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Test Connection - Test.ps1

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
42,146
Location:
Prague, Czechia

Re: Connect to SFTP that uses passphrase with PowerShell script

It looks like a networking problem (nothing to do with the passphrase).
Can you connect to that SFTP server anyhow from the machine where your code fails? From WinSCP GUI? From any other SFTP client? If you can, post both log from the code and from the client.

Reply with quote

Kell
Guest

Re: Connect to SFTP that uses passphrase with PowerShell script

Yes. By GUI I can connect to the SFTP writing passphrase on the dialog box.
This is the log on the PowerShell script:
. 2025-06-19 14:04:24.712 --------------------------------------------------------------------------
. 2025-06-19 14:04:24.736 WinSCP Version 5.13.4 (Build 8731) (OS 10.0.26100 - Windows 10 Enterprise)
. 2025-06-19 14:04:24.760 Configuration: nul
. 2025-06-19 14:04:24.785 Log level: Normal
. 2025-06-19 14:04:24.808 Local account: XXXXXXX\vendor53
. 2025-06-19 14:04:24.831 Working directory: \\server\Reports\WinSCP
. 2025-06-19 14:04:24.855 Process ID: 24264
. 2025-06-19 14:04:24.878 Command-line: "\server\Reports\WinSCP\winscp.exe" /xmllog="C:\Users\vendor53\AppData\Local\Temp\wscp1E54.00C9E518.tmp" /xmlgroups /xmllogrequired /nointeractiveinput /dotnet=5.13.4  /ini=nul /log="\server\Reports\WinSCP\Prime\log.txt"  /console /consoleinstance=_7764_40550289_53
. 2025-06-19 14:04:24.904 Time zone: Current: GMT-6 (Central Standard Time (Mexico)), No DST
. 2025-06-19 14:04:24.952 Login time: Thursday, June 19, 2025 2:04:24 PM
. 2025-06-19 14:04:24.998 --------------------------------------------------------------------------
. 2025-06-19 14:04:25.046 Script: Retrospectively logging previous script records:
> 2025-06-19 14:04:25.129 Script: option batch on
< 2025-06-19 14:04:25.174 Script: batch           on        
< 2025-06-19 14:04:25.222 Script: reconnecttime   120       
> 2025-06-19 14:04:25.269 Script: option confirm off
< 2025-06-19 14:04:25.316 Script: confirm         off       
> 2025-06-19 14:04:25.364 Script: option reconnecttime 120
< 2025-06-19 14:04:25.410 Script: reconnecttime   120       
> 2025-06-19 14:04:25.434 Script: open sftp://server@server.com:22 -hostkey="ssh-rsa 2048 9p:26:c0:7d:9b:a1:5f:cb:a7:pp:68:bb:91:pp:pp:6a" -privatekey="\server\Reports\Prime\prime.ppk" -timeout=15 -passphrase=***
. 2025-06-19 14:04:25.493 --------------------------------------------------------------------------
. 2025-06-19 14:04:25.576 Session name: server@server.com (Ad-Hoc site)
. 2025-06-19 14:04:25.622 Host name: server.com (Port: 22)
. 2025-06-19 14:04:25.670 User name: usersftp(Password: No, Key file: Yes, Passphrase: Yes)
. 2025-06-19 14:04:25.741 Tunnel: No
. 2025-06-19 14:04:25.789 Transfer Protocol: SFTP
. 2025-06-19 14:04:25.894 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
. 2025-06-19 14:04:25.918 Disable Nagle: No
. 2025-06-19 14:04:25.941 Proxy: None
. 2025-06-19 14:04:25.964 Send buffer: 262144
. 2025-06-19 14:04:25.988 SSH protocol version: 2; Compression: No
. 2025-06-19 14:04:26.010 Bypass authentication: No
. 2025-06-19 14:04:26.035 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: Yes
. 2025-06-19 14:04:26.058 GSSAPI: Forwarding: No; Libs: gssapi32,sspi,custom; Custom: 
. 2025-06-19 14:04:26.082 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2025-06-19 14:04:26.104 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1
. 2025-06-19 14:04:26.128 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
. 2025-06-19 14:04:26.152 Simple channel: Yes
. 2025-06-19 14:04:26.176 Return code variable: Autodetect; Lookup user groups: Auto
. 2025-06-19 14:04:26.199 Shell: default
. 2025-06-19 14:04:26.222 EOL: LF, UTF: Auto
. 2025-06-19 14:04:26.245 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
. 2025-06-19 14:04:26.270 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2025-06-19 14:04:26.293 SFTP Bugs: Auto,Auto
. 2025-06-19 14:04:26.318 SFTP Server: default
. 2025-06-19 14:04:26.341 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2025-06-19 14:04:26.365 Cache directory changes: Yes, Permanent: Yes
. 2025-06-19 14:04:26.389 Recycle bin: Delete to: No, Overwritten to: No, Bin path: 
. 2025-06-19 14:04:26.412 DST mode: Unix
. 2025-06-19 14:04:26.435 --------------------------------------------------------------------------
. 2025-06-19 14:04:26.458 Looking up host "server.com" for SSH connection
. 2025-06-19 14:04:26.582 Connecting to 208.90.130.50 port 22
. 2025-06-19 14:04:47.687 Failed to connect to 208.90.130.50: Network error: Connection timed out
. 2025-06-19 14:04:47.806 Knocking FTP port.
. 2025-06-19 14:04:49.872 FTP port did not open.

Reply with quote

martin
Site Admin
martin avatar

Re: Connect to SFTP that uses passphrase with PowerShell script

Thanks, though please "post both log from the code and from the client". Also please use the latest version. WinSCP 5.13.4 is 7 years old.

Reply with quote

Advertisement

You can post new topics in this forum