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
1
u/roxalu Jun 19 '26
Quoting the WinSCP documentation at https://winscp.net/eng/docs/library_session_putfiles :
remotepath: „Full path to upload the file to. When uploading multiple files, the filename in the path should be replaced with operation mask or omitted (path ends with slash)“
So your argument value "/*“ means, you upload to root folder of remote file system, and use a so called by WinSCP „operation mask“ of „*“, which uses all characters of local filename also for the remote write. In this case, it is more usual to NOT specify any operation mask.
Also be aware that the root path seen by your remote sftp session may be a „chroot“ - not necessarily the same as seen by other users on same system.