Answer the question
In order to leave comments, you need to log in
How to send a stream to ftp in Windows?
Tell me how you can encrypt a file and send it to ftp without saving the intermediate result to disk in Powershell?
So far it turned out like this, but it saves the intermediate result, it is necessary that it pours directly to ftp.
Thank you.
$str = "'C:\Program Files\OpenSSL\bin\openssl.exe' enc -e -aes-256-cbc -a -in '" + $file.Fullname + "' -out '" + $encrDest + $file + ".enc' -k 'key'"
Invoke-Expression "& $str"
Add-Type -Path "C:\Scripts\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "7.7.7.7"
UserName = "user"
Password = "pass"
}
try
{
# Connect
$session = New-Object WinSCP.Session
$session.Open($sessionOptions)
# List files
$remotePath = "/"
$directoryInfo = $session.ListDirectory($remotePath)
# Find old files
$limit = (Get-Date).AddDays(-1)
$oldFiles =
$directoryInfo.Files |
#Where-Object { -Not $_.IsDirectory } |
Where-Object { $_.LastWriteTime -lt $limit }
$oldFiles
# Delete them
foreach ($oldFileInfo in $oldFiles)
{
if ($oldFileInfo.Name -ne '..') {
$oldFilePath = $session.EscapeFileMask($remotePath + "/" + $oldFileInfo.Name)
$session.RemoveFiles($oldFilePath).Check()
}
}
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult = $session.PutFiles($encrDest, "/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
}
Write-Host "Done"
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question