I'm trying to automate the transfer of files with WinSCP using PowerShell. I've installed .NET assembly. In the code I've added, I've taken out credentials and hostname for privacy and I'm also using the full path to WinSCPnet.dll
. Here is the error I'm getting:
Error: The value supplied is not valid, or the property is read-only. Change the value, and then try again.
I'm getting this where $sessionOptions
is defined on 5th line. Any ideas of what I could do to get this running?
try
{
Add-Type -Path "WinSCPnet.dll"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "HostName"
UserName = "UserName"
Password = "Password"
SshHostKeyFingerprint = "ssh-rsa 2048 Fingerprint here"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.PutFiles("Source", "Destination", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}