I am trying to use PowerShell to upload files using an encrypted passphrase. I keep getting a
"Wrong passphrase" error. I used the template that the WinSCP GUI provided and added the
PrivateKeyPassphrase
property.
Here is the commands I used to create the encrypted passphrase:
$Secure = Read-Host -AsSecureString
$Encrypted = ConvertFrom-SecureString -SecureString $Secure
$Encrypted | Set-Content Encrypted.txt
Here is the PowerShell code (I obfuscated user and key information):
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
UserName = "xxxxxxxx"
SshHostKeyFingerprint = "ssh-rsa 2048 uKAqf7+...TvbBFrEkyxY="
SshPrivateKeyPath = "C:\Projects\PrivateKey\PrivateKey.ppk"
PrivateKeyPassphrase = ConvertTo-SecureString "01000000d08c9ddf0115...719f9ddf1d944d7cd5e"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Your code
}
finally
{
$session.Dispose()
}
Here is the response:
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):
Using username "xxxxxxxx".
Authenticating with public key "rsa-key-20200313".
Wrong passphrase.
Authentication failed."
At C:\Projects\PowerShell\PS-11.ps1:19 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException