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
View all comments
Show parent comment
2
u/sid351 Jun 15 '26
Have you tried running the steps of the script manually, or using the debugging tools in Visual Studio Code (or the PowerShell ISE)?
That might make the error more obvious.
Also, just to check, are your manual uploads and the script uploads going to the same place on the remote side, and do they use the exact same credentials?