当WinSCP GUI工作时,PowerShell中的WinSCP .NET程序集无法通过密码进行身份验证

时间:2019-05-26 16:13:06

标签: powershell sftp winscp winscp-net

我正在尝试使用PowerShell通过SFTP自动传输文件。 SFTP连接需要公用/专用密钥和用户名/密码。我正在使用WinSCP在PowerShell中进行传输。通过密钥进行身份验证之后,此操作将立即失败。它似乎与需要交互模式有关。如果我使用FileZilla手动执行此操作,则必须在“交互”模式下执行此操作,并在交换密钥以建立连接后输入密码。

Add-Type -Path "WinSCPnet.dll"

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Sftp
    HostName = "stage.sample.com"
    PortNumber= 22
    UserName = "MyUsername"
    Password = "MyPassword"
    SshHostKeyFingerprint = "ssh-rsa 1024 znVXTS2xndXS4dKADWyRUUzImR/kG1Ipojlr2eL2765="
    SshPrivateKeyPath="D:\PrivateKeys\private.ppk"
    PrivateKeyPassphrase="MyPrivateKeypassword"
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)
    # Your code
    $session.PutFiles("d:\6201_2019-04-15.zip", "\6201_2019-04-15.zip")
}
finally
{
    $session.Dispose()
}

使用用户界面时WinSCP日志有效,

. 2019-05-28 09:35:25.692 --------------------------------------------------------------------------
. 2019-05-28 09:35:25.692 WinSCP Version 5.15.1 (Build 9407) (OS 6.1.7601 Service Pack 1 - Windows Server 2008 R2 Standard)
. 2019-05-28 09:35:25.692 Configuration: D:\Ei Dynamics\sftp\WinSCP.ini
. 2019-05-28 09:35:25.693 Log level: Normal
. 2019-05-28 09:35:25.693 Local account: B\a_xyz
. 2019-05-28 09:35:25.693 Working directory: D:\Ei Dynamics\sftp
. 2019-05-28 09:35:25.693 Process ID: 11292
. 2019-05-28 09:35:25.693 Command-line: "D:\Ei Dynamics\sftp\WinSCP.exe" 
. 2019-05-28 09:35:25.694 Time zone: Current: GMT-5, Standard: GMT-6 (Central Standard Time), DST: GMT-5 (Central Daylight Time), DST Start: 3/10/2019, DST End: 11/3/2019
. 2019-05-28 09:35:25.694 Login time: Tuesday, May 28, 2019 9:35:25 AM
. 2019-05-28 09:35:25.694 --------------------------------------------------------------------------
. 2019-05-28 09:35:25.694 Session name: MyUsername@stage.example.com (Modified site)
. 2019-05-28 09:35:25.694 Host name: stage.example.com (Port: 10022)
. 2019-05-28 09:35:25.694 User name: MyUsername (Password: Yes, Key file: Yes, Passphrase: No)
. 2019-05-28 09:35:25.694 Tunnel: No
. 2019-05-28 09:35:25.694 Transfer Protocol: SFTP (SCP)
. 2019-05-28 09:35:25.694 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
. 2019-05-28 09:35:25.694 Disable Nagle: No
. 2019-05-28 09:35:25.694 Proxy: None
. 2019-05-28 09:35:25.694 Send buffer: 262144
. 2019-05-28 09:35:25.694 SSH protocol version: 2; Compression: No
. 2019-05-28 09:35:25.694 Bypass authentication: No
. 2019-05-28 09:35:25.694 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: Yes
. 2019-05-28 09:35:25.694 GSSAPI: Forwarding: No; Libs: gssapi32,sspi,custom; Custom: 
. 2019-05-28 09:35:25.694 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2019-05-28 09:35:25.694 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1
. 2019-05-28 09:35:25.694 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
. 2019-05-28 09:35:25.694 Simple channel: Yes
. 2019-05-28 09:35:25.694 Return code variable: Autodetect; Lookup user groups: Auto
. 2019-05-28 09:35:25.694 Shell: default
. 2019-05-28 09:35:25.694 EOL: LF, UTF: Auto
. 2019-05-28 09:35:25.694 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
. 2019-05-28 09:35:25.694 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No; Exit code 1 is error: No
. 2019-05-28 09:35:25.694 SFTP Bugs: Auto,Auto
. 2019-05-28 09:35:25.694 SFTP Server: default
. 2019-05-28 09:35:25.694 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2019-05-28 09:35:25.694 Cache directory changes: Yes, Permanent: Yes
. 2019-05-28 09:35:25.694 Recycle bin: Delete to: No, Overwritten to: No, Bin path: 
. 2019-05-28 09:35:25.694 DST mode: Unix
. 2019-05-28 09:35:25.694 --------------------------------------------------------------------------
. 2019-05-28 09:35:25.723 Looking up host "stage.example.com" for SSH connection
. 2019-05-28 09:35:25.724 Connecting to 192.123.100.123 port 10022
. 2019-05-28 09:35:25.747 We claim version: SSH-2.0-WinSCP_release_5.15.1
. 2019-05-28 09:35:25.778 Server version: SSH-2.0-Maverick_SSHD
. 2019-05-28 09:35:25.778 Using SSH protocol version 2
. 2019-05-28 09:35:25.783 Have a known host key of type rsa2
. 2019-05-28 09:35:25.785 Doing Diffie-Hellman group exchange
. 2019-05-28 09:35:25.875 Doing Diffie-Hellman key exchange with hash SHA-1
. 2019-05-28 09:35:26.106 Host key fingerprint is:
. 2019-05-28 09:35:26.106 ssh-rsa 1024 e6:c8:78:79:f9:d4:e9:af:f2:53:28:6d:87:c8:62:f5 znVXTS2xndXS4dKADWyRUUzImR/kG1Ipojlr2eL1234=
. 2019-05-28 09:35:26.111 Host key matches cached key
. 2019-05-28 09:35:26.113 Initialised AES-256 CBC client->server encryption
. 2019-05-28 09:35:26.113 Initialised HMAC-SHA1 client->server MAC algorithm
. 2019-05-28 09:35:26.113 Initialised AES-256 CBC server->client encryption
. 2019-05-28 09:35:26.113 Initialised HMAC-SHA1 server->client MAC algorithm
. 2019-05-28 09:35:26.196 Reading key file "D:\privateKeys\myprivate.ppk"
! 2019-05-28 09:35:26.196 Using username "MyUsername".
. 2019-05-28 09:35:31.197 Server offered these authentication methods: password,publickey,keyboard-interactive
. 2019-05-28 09:35:31.197 Offered public key
. 2019-05-28 09:35:31.247 Offer of public key accepted
! 2019-05-28 09:35:31.247 Authenticating with public key "rsa-key-20190313"
. 2019-05-28 09:35:31.262 Prompt (passphrase, "SSH key passphrase", <no instructions>, "Passphrase for key "rsa-key-20190313": ")
. 2019-05-28 09:35:33.837 Sent public key signature
! 2019-05-28 09:35:33.864 Further authentication required
. 2019-05-28 09:35:33.869 Further authentication required
. 2019-05-28 09:35:33.869 Server offered these authentication methods: password,keyboard-interactive8ƒÆqtY"Ç4¡FØwFα[D*ó6çeÊNô§1‚>°
. 2019-05-28 09:35:33.869 Attempting keyboard-interactive authentication
. 2019-05-28 09:35:33.895 Prompt (keyboard interactive, "SSH server: password", "Using keyboard-interactive authentication.
. 2019-05-28 09:35:33.895 Enter Password for MyUsername:", "Password:")
. 2019-05-28 09:35:33.895 Using stored password.
. 2019-05-28 09:35:33.958 Access granted
. 2019-05-28 09:35:33.958 Opening session as main channel
. 2019-05-28 09:35:33.982 Opened main channel
. 2019-05-28 09:35:34.149 Started a shell/command
. 2019-05-28 09:35:34.157 --------------------------------------------------------------------------
. 2019-05-28 09:35:34.158 Using SFTP protocol.
. 2019-05-28 09:35:34.160 Doing startup conversation with host.
> 2019-05-28 09:35:34.170 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2019-05-28 09:35:34.193 Type: SSH_FXP_VERSION, Size: 33, Number: -1
. 2019-05-28 09:35:34.193 SFTP version 3 negotiated.
. 2019-05-28 09:35:34.193 Unknown server extension newline@vandyke.com="\n"
. 2019-05-28 09:35:34.193 We believe the server has signed timestamps bug
. 2019-05-28 09:35:34.193 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 strings are not mandatory
. 2019-05-28 09:35:34.193 Getting current directory name.
. 2019-05-28 09:35:34.193 Getting real path for '.'
> 2019-05-28 09:35:34.193 Type: SSH_FXP_REALPATH, Size: 10, Number: 2832
< 2019-05-28 09:35:34.226 Type: SSH_FXP_NAME, Size: 51, Number: 2832
. 2019-05-28 09:35:34.226 Real path is '/'
. 2019-05-28 09:35:34.267 Listing directory "/".
> 2019-05-28 09:35:34.267 Type: SSH_FXP_OPENDIR, Size: 10, Number: 3083
< 2019-05-28 09:35:34.296 Type: SSH_FXP_HANDLE, Size: 10, Number: 3083
> 2019-05-28 09:35:34.296 Type: SSH_FXP_READDIR, Size: 10, Number: 3340
< 2019-05-28 09:35:34.394 Type: SSH_FXP_NAME, Size: 112, Number: 3340
> 2019-05-28 09:35:34.394 Type: SSH_FXP_READDIR, Size: 10, Number: 3596
< 2019-05-28 09:35:34.426 Type: SSH_FXP_STATUS, Size: 45, Number: 3596
< 2019-05-28 09:35:34.426 Status code: 1
> 2019-05-28 09:35:34.426 Type: SSH_FXP_CLOSE, Size: 10, Number: 3844
. 2019-05-28 09:35:34.426 3098;d;0;2019-05-28T14:35:34.000Z;3;"300" [300];"100" [100];rwx------;0
. 2019-05-28 09:35:34.426 ..;D;0;1899-12-30T06:00:00.000Z;0;"" [0];"" [0];---------;0
. 2019-05-28 09:35:34.482 Startup conversation with host finished.
. 2019-05-28 09:35:50.983 Received disconnect message (by application)
. 2019-05-28 09:35:50.983 Disconnection message text: Idle connection
. 2019-05-28 09:35:50.983 Server sent disconnect message
. 2019-05-28 09:35:50.983 type 11 (by application):
. 2019-05-28 09:35:50.983 "Idle connection"
. 2019-05-28 09:38:36.501 Received disconnect message (by application)
. 2019-05-28 09:38:36.501 Disconnection message text: Idle connection
. 2019-05-28 09:38:36.501 Server sent disconnect message
. 2019-05-28 09:38:36.501 type 11 (by application):
. 2019-05-28 09:38:36.501 "Idle connection"
* 2019-05-28 09:38:36.501 (ESshFatal) Server sent disconnect message
* 2019-05-28 09:38:36.501 type 11 (by application):
* 2019-05-28 09:38:36.501 "Idle connection"
. 2019-05-28 09:38:45.572 Looking up host "stage.example.com" for SSH connection
. 2019-05-28 09:38:45.573 Connecting to 192.123.100.123 port 10022
. 2019-05-28 09:38:45.595 We claim version: SSH-2.0-WinSCP_release_5.15.1
. 2019-05-28 09:38:45.619 Server version: SSH-2.0-Maverick_SSHD
. 2019-05-28 09:38:45.619 Using SSH protocol version 2
. 2019-05-28 09:38:45.621 Have a known host key of type rsa2
. 2019-05-28 09:38:45.622 Doing Diffie-Hellman group exchange
. 2019-05-28 09:38:45.717 Doing Diffie-Hellman key exchange with hash SHA-1
. 2019-05-28 09:38:45.865 Host key fingerprint is:
. 2019-05-28 09:38:45.865 ssh-rsa 1024 e6:c8:78:79:f9:d4:e9:af:f2:53:28:6d:87:c8:62:f5 znVXTS2xndXS4dKADWyRUUzImR/kG1Ipojlr2e1234=
. 2019-05-28 09:38:45.910 Host key matches cached key
. 2019-05-28 09:38:45.911 Initialised AES-256 CBC client->server encryption
. 2019-05-28 09:38:45.911 Initialised HMAC-SHA1 client->server MAC algorithm
. 2019-05-28 09:38:45.911 Initialised AES-256 CBC server->client encryption
. 2019-05-28 09:38:45.911 Initialised HMAC-SHA1 server->client MAC algorithm
. 2019-05-28 09:38:45.996 Reading key file "D:\privateKeys\myprivate.ppk"
! 2019-05-28 09:38:45.996 Using username "MyUsername".
. 2019-05-28 09:39:19.037 Received disconnect message (by application)
. 2019-05-28 09:39:19.037 Disconnection message text: Idle connection
. 2019-05-28 09:39:19.037 Server sent disconnect message
. 2019-05-28 09:39:19.037 type 11 (by application):
. 2019-05-28 09:39:19.037 "Idle connection"

Powershell日志文件

. 2019-05-28 09:37:41.686 --------------------------------------------------------------------------
. 2019-05-28 09:37:41.686 WinSCP Version 5.15.1 (Build 9407) (OS 6.1.7601 Service Pack 1 - Windows Server 2008 R2 Standard)
. 2019-05-28 09:37:41.686 Configuration: nul
. 2019-05-28 09:37:41.686 Log level: Normal
. 2019-05-28 09:37:41.686 Local account: B\a_xyz
. 2019-05-28 09:37:41.686 Working directory: D:\Ei Dynamics\sftp
. 2019-05-28 09:37:41.686 Process ID: 132
. 2019-05-28 09:37:41.687 Command-line: "D:\Ei Dynamics\sftp\winscp.exe" /xmllog="C:\Users\a_xyz\AppData\Local\Temp\13\wscp2EA0.01871C8A.tmp" /xmlgroups /xmllogrequired /nointeractiveinput /dotnet=5.15.1  /ini=nul /log="D:\Ei Dynamics\sftp\powershell.log"  /console /consoleinstance=_11936_48002702_723
. 2019-05-28 09:37:41.687 Time zone: Current: GMT-5, Standard: GMT-6 (Central Standard Time), DST: GMT-5 (Central Daylight Time), DST Start: 3/10/2019, DST End: 11/3/2019
. 2019-05-28 09:37:41.687 Login time: Tuesday, May 28, 2019 9:37:41 AM
. 2019-05-28 09:37:41.687 --------------------------------------------------------------------------
. 2019-05-28 09:37:41.687 Script: Retrospectively logging previous script records:
> 2019-05-28 09:37:41.687 Script: option batch on
< 2019-05-28 09:37:41.687 Script: batch           on        
< 2019-05-28 09:37:41.687 Script: reconnecttime   120       
> 2019-05-28 09:37:41.687 Script: option confirm off
< 2019-05-28 09:37:41.687 Script: confirm         off       
> 2019-05-28 09:37:41.687 Script: option reconnecttime 120
< 2019-05-28 09:37:41.687 Script: reconnecttime   120       
> 2019-05-28 09:37:41.687 Script: open sftp://MyUsername:***@stage.example.com:10022 -hostkey="ssh-rsa 1024 znVXTS2xndXS4dKADWyRUUzImR/kG1Ipojlr2eL1234=" -privatekey="D:\PrivateKeys\myprivate.ppk" -timeout=15 -passphrase=***
. 2019-05-28 09:37:41.687 --------------------------------------------------------------------------
. 2019-05-28 09:37:41.687 Session name: MyUsername@stage.example.com (Ad-Hoc site)
. 2019-05-28 09:37:41.687 Host name: stage.example.com (Port: 10022)
. 2019-05-28 09:37:41.688 User name: MyUsername (Password: Yes, Key file: Yes, Passphrase: Yes)
. 2019-05-28 09:37:41.688 Tunnel: No
. 2019-05-28 09:37:41.688 Transfer Protocol: SFTP
. 2019-05-28 09:37:41.688 Ping type: Off, Ping interval: 30 sec; Timeout: 15 sec
. 2019-05-28 09:37:41.688 Disable Nagle: No
. 2019-05-28 09:37:41.688 Proxy: None
. 2019-05-28 09:37:41.688 Send buffer: 262144
. 2019-05-28 09:37:41.688 SSH protocol version: 2; Compression: No
. 2019-05-28 09:37:41.688 Bypass authentication: No
. 2019-05-28 09:37:41.688 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: Yes
. 2019-05-28 09:37:41.688 GSSAPI: Forwarding: No; Libs: gssapi32,sspi,custom; Custom: 
. 2019-05-28 09:37:41.688 Ciphers: aes,chacha20,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2019-05-28 09:37:41.688 KEX: ecdh,dh-gex-sha1,dh-group14-sha1,rsa,WARN,dh-group1-sha1
. 2019-05-28 09:37:41.688 SSH Bugs: Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto,Auto
. 2019-05-28 09:37:41.688 Simple channel: Yes
. 2019-05-28 09:37:41.688 Return code variable: Autodetect; Lookup user groups: Auto
. 2019-05-28 09:37:41.688 Shell: default
. 2019-05-28 09:37:41.688 EOL: LF, UTF: Auto
. 2019-05-28 09:37:41.688 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes; Follow directory symlinks: No
. 2019-05-28 09:37:41.688 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No; Exit code 1 is error: No
. 2019-05-28 09:37:41.688 SFTP Bugs: Auto,Auto
. 2019-05-28 09:37:41.688 SFTP Server: default
. 2019-05-28 09:37:41.688 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2019-05-28 09:37:41.688 Cache directory changes: Yes, Permanent: Yes
. 2019-05-28 09:37:41.688 Recycle bin: Delete to: No, Overwritten to: No, Bin path: 
. 2019-05-28 09:37:41.688 DST mode: Unix
. 2019-05-28 09:37:41.688 --------------------------------------------------------------------------
. 2019-05-28 09:37:41.689 Looking up host "stage.example.com" for SSH connection
. 2019-05-28 09:37:41.736 Connecting to 192.123.100.123 port 10022
. 2019-05-28 09:37:41.757 We claim version: SSH-2.0-WinSCP_release_5.15.1
. 2019-05-28 09:37:41.782 Server version: SSH-2.0-Maverick_SSHD
. 2019-05-28 09:37:41.782 Using SSH protocol version 2
. 2019-05-28 09:37:41.782 Have a known host key of type rsa2
. 2019-05-28 09:37:41.782 Doing Diffie-Hellman group exchange
. 2019-05-28 09:37:41.872 Doing Diffie-Hellman key exchange with hash SHA-1
. 2019-05-28 09:37:42.039 Host key fingerprint is:
. 2019-05-28 09:37:42.039 ssh-rsa 1024 e6:c8:78:79:f9:d4:e9:af:f2:53:28:6d:87:c8:62:f5 znVXTS2xndXS4dKADWyRUUzImR/kG1Ipojlr2eL1234=
. 2019-05-28 09:37:42.040 Host key matches configured key
. 2019-05-28 09:37:42.040 Initialised AES-256 CBC client->server encryption
. 2019-05-28 09:37:42.040 Initialised HMAC-SHA1 client->server MAC algorithm
. 2019-05-28 09:37:42.040 Initialised AES-256 CBC server->client encryption
. 2019-05-28 09:37:42.040 Initialised HMAC-SHA1 server->client MAC algorithm
. 2019-05-28 09:37:42.125 Reading key file "D:\PrivateKeys\myprivate.ppk"
! 2019-05-28 09:37:42.126 Using username "MyUsername".
. 2019-05-28 09:37:42.149 Server offered these authentication methods: password,publickey,keyboard-interactive
. 2019-05-28 09:37:42.149 Offered public key
. 2019-05-28 09:37:42.201 Offer of public key accepted
! 2019-05-28 09:37:42.201 Authenticating with public key "rsa-key-20190313"
. 2019-05-28 09:37:42.202 Prompt (passphrase, "SSH key passphrase", <no instructions>, "Passphrase for key "rsa-key-20190313": ")
. 2019-05-28 09:37:42.202 Using configured passphrase.
. 2019-05-28 09:37:42.330 Sent public key signature
! 2019-05-28 09:37:42.354 Further authentication required
. 2019-05-28 09:37:42.355 Further authentication required
. 2019-05-28 09:37:42.355 Server offered these authentication methods: password,keyboard-interactive}®”Çÿ®UÆ'Éî€ûw‘BÊMNŽoKÓU/W
. 2019-05-28 09:37:42.355 Attempting keyboard-interactive authentication
. 2019-05-28 09:37:42.377 Prompt (keyboard interactive, "SSH server: password", "Using keyboard-interactive authentication.
. 2019-05-28 09:37:42.377 Enter Password for MyUsername:", "Password:")
. 2019-05-28 09:37:42.377 Using stored password.
. 2019-05-28 09:37:42.432 Keyboard-interactive authentication failed
! 2019-05-28 09:37:42.432 Access denied
. 2019-05-28 09:37:42.436 Server offered these authentication methods: password,keyboard-interactive
. 2019-05-28 09:37:42.436 Attempting keyboard-interactive authentication
. 2019-05-28 09:37:42.458 Prompt (keyboard interactive, "SSH server: password", "Using keyboard-interactive authentication.
. 2019-05-28 09:37:42.458 Invalid Password, reenter Password for MyUsername:", "Password:")
. 2019-05-28 09:37:42.458 Disconnected: Unable to authenticate

1 个答案:

答案 0 :(得分:0)

问题是密码中包含$。一旦他们按照@MartinPrikry的建议逃脱了,它就解决了问题,一切都可以正常工作。