Post a reply

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Automating passphrase authentication in script


This is my first post so apologies if I leave out anything important.
I'm also very new to SFTP and scripting in general so please bear with me :).

I'm trying to edit a PS script to pull files from a clients SFTP server everyday at a certain time. I have a scheduled task set to run the script.

This works for clients that have a private key with no passphrase on it, but for any client that has a private key with a passphrase it does not work.

I did manage to get it working if I load the key with the passphrase using Pageant, but this is very fragile as you have to keep entering the passphrase whenever it reboots (Unless im doing something wrong).

I was wondering if there was a way to integrate the passphrase into my script,below is what I have so far.
Some context for the script, I didn't write it, I think the previous IT provider took it from github and changed it up.
The xxxx parts are where ive taken out sensitive info.
the password variable and credential variable, I'm guessing these are not necessary as we are using private key authentication. I did try using this to automate the passphrase with no success.

$Username = "xxxxxx"
$Source = "xxxxxx"
$Destination = "F:\FTPServer\xxxxxx\testing"
$password = ConvertTo-SecureString "xxxxxx" -AsPlainText -Force
$FTPRemoteName = "xxxxx"
$FingerPrint = "xxxx"
$SshPrivateKeyPath = "F:\Private keys\xxxx\private key.ppk"
$credential = New-Object System.Management.Automation.PSCredential $Username,$password
# Specify Session Options
$sessionOption = New-WinSCPSessionOption -Hostname $FTPRemoteName -Credential $credential -Protocol Sftp -SshHostKeyFingerprint $FingerPrint -SshPrivateKeyPath $SshPrivateKeyPath
# Open the session
$session = New-WinSCPSession -SessionOption $sessionOption
Sync-WinSCPPath -LocalPath $Destination -RemotePath $source -Mode Local
# Close the session
Remove-WinSCPSession $session

Help would be greatly appreciated.

Thank you very much! :)