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()
}
10
Upvotes
View all comments
Show parent comment
3
u/dodexahedron Jun 15 '26 edited Jun 15 '26
Windows itself already has openssh.
No module needed at all.
scp -p 2222 ./*.ext account@server.domain.tld:path/from/home/
It can do nearly everything the linux version of openssh can, aside from single-socket multi-session multiplexing (which really doesnt matter for this use case anyway)
scp, by the way, uses sftp by default. You actually have to pass an option to force scp protocol.
There is also an explicit sftp executable you can call, but the intent of it is to be interactive, like traditional ftp shells, and it can't do anything that scp can't do non-interqctively.
Just use the built in scp command.