r/PowerShell • u/DoktorLuciferWong • Jun 15 '26
Question winscp/powershell sftp upload script, having trouble getting it working
The issues I'm experiencing are as follows:
- Script appears to run to completion, and gives me "success" message at the end.
- The file is not actually uploaded/written to the server. If the file already exists, it is not updated, and has the old date modified/file size. Refreshing does not update them.
- I can upload files manually to the server, but I do not appear to have permissions to do anything else (delete, move, rename, etc)
What am I doing wrong?
Below is a version of the script, but stripped of all identifying variable names/values. I am also aware that I shouldn't use plaintext passwords and that I shouldn't use "GiveUpSecurityAndAcceptAny"
Add-Type -Path "$PSScriptRoot\WinSCPnet.dll"
$conf = Import-PowerShellDataFile -Path $env:ConfFile
$Data = Invoke-Sqlcmd @conf -Inputfile "$PSScriptRoot\query.sql"
$Data | Export-CSV -Path "$PSScriptRoot\query.csv" -Delimiter "`t"
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::sftp
Hostname = "ftp.host.com"
Username = "user111"
Password = "genericpassword"
PortNumber = 2222
SshHostKeyPolicy = [WinSCP.SshHostKeyPolicy]::GiveUpSecurityAndAcceptAny
}
Write-Host @sessionOptions
$session = New-Object WinSCP.Session
try {
$session.Open($sessionOptions)
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
$transferResult = $session.PutFiles("$PSScriptRoot\data.csv", ".\", $False, $transferOptions)
$transferResult.Check()
foreach($transfer in $transferResult.Transfers) {
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
finally {
$session.Dispose()
}
9
Upvotes
5
u/Rowf Jun 15 '26
Sorry this doesn’t answer your specific question, but WinSCP has its own scripting ability. I found it easier to do the heavy sftp lifting and let powershell handle the rest.
4
u/BlackV Jun 15 '26 edited Jun 15 '26
Your step 3
Is that not your issue? What makes you think it's the script?
If you use a random name and upload what happens?
You also have 2 CVS in your code but only 1 is uploaded? Have you actually stepped through the code line by line to confirm what/where your issues are?
1
u/DoktorLuciferWong Jun 15 '26
I have found the solution. I was basically passing a wrong argument to PutFiles (specifically, the second argument)
Instead of
I should have used
I am not sure why this is the case, when a number of my other scripts just use "." and work just fine. If anyone could offer an explanation, I'd appreciate it. I think I'm just misunderstanding what the meaning/usage of the second argument is
1
u/tschugger Jun 16 '26
Rsync or reclone may be your friend
2
u/Fatel28 Jun 15 '26
Powershell has an ssh module (posh-ssh) that works with sftp, no winscp needed. I'd just use that personally
2
u/adrach87 Jun 15 '26
You don't have a catch block. Is your script just silently eating the error, calling dispose in your finally and closing?
Also, you can just use the command line version of winscp and call that with PowerShell. I've done that successfully in the past.
2
u/markdmac Jun 15 '26
It's been a while since I automated a connection with WinSCP, but the one thing that I remember is I needed to generate within WinSCP the connection string and I don't see that in your code. They concatenate the address along with the password and I don't believe the way you have it scripted will work depending on the target security settings.