Differences
This shows you the differences between the selected revisions of the page.
script_upload_file_list 2015-04-14 | script_upload_file_list 2022-06-16 (current) | ||
Line 11: | Line 11: | ||
# Setup session options | # Setup session options | ||
- | $sessionOptions = New-Object WinSCP.SessionOptions | + | $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ |
- | ···$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp | + | ········Protocol = [WinSCP.Protocol]::Sftp |
- | $sessionOptions.HostName = "example.com" | + | ·······HostName = "example.com" |
- | $sessionOptions.UserName = "user" | + | ·······UserName = "user" |
- | $sessionOptions.Password = "mypassword" | + | ·······Password = "mypassword" |
- | $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | ·······SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
+ | } | ||
$session = New-Object WinSCP.Session | $session = New-Object WinSCP.Session | ||
Line 31: | Line 32: | ||
foreach ($line in $lines) | foreach ($line in $lines) | ||
{ | { | ||
- | Write-Host ("Uploading {0} ..." -f $line) | + | Write-Host "Uploading $line ..." |
$session.PutFiles($line, $remotePath).Check() | $session.PutFiles($line, $remotePath).Check() | ||
} | } | ||
Line 43: | Line 44: | ||
exit 0 | exit 0 | ||
} | } | ||
- | catch [Exception] | + | catch |
{ | { | ||
- | Write-Host $_.Exception.Message | + | Write-Host "Error: $($_.Exception.Message)" |
exit 1 | exit 1 | ||
} | } | ||
</code> | </code> | ||
- | ===== Using WinSCP Scripting ===== | + | ===== [[scripting]] Using WinSCP Scripting ===== |
- | You may use following batch file that calls WinSCP [[scripting|script]]: | + | You may use following batch file that calls generated WinSCP [[scripting|script]]: |
<code batch> | <code batch> | ||
@echo off | @echo off | ||
- | set SESSION=sftp://user:password@example.com/ | + | set SESSION=sftp://user:password;fingerprint=ssh-rsa-xxxxxxxxxxx...@example.com/ |
set REMOTE_PATH=/home/user/ | set REMOTE_PATH=/home/user/ | ||
- | echo option batch abort > script.tmp | + | ( |
- | echo option confirm off >> script.tmp | + | echo open %SESSION% |
- | echo open %SESSION% >> script.tmp | + | ··echo <nohilite>cd</nohilite> %REMOTE_PATH% |
- | rem Generate "put" command for each line in list file | + | ··rem Generate "put" command for each line in list file |
- | for /F %i in (list.txt) do echo put "%i" "%REMOTE_PATH%" >> script.tmp | + | ··for /F %%i in (list.txt) do echo put "%%i" |
- | echo exit·>> script.tmp | + | ··echo <nohilite>exit</nohilite> |
+ | ) > script.tmp | ||
- | winscp.com /script=script.tmp | + | winscp.com /ini=nul /log=script.log /script=script.tmp |
set RESULT=%ERRORLEVEL% | set RESULT=%ERRORLEVEL% | ||
Line 73: | Line 75: | ||
rem Propagating WinSCP exit code | rem Propagating WinSCP exit code | ||
- | exit %RESULT% | + | exit /b %RESULT% |
</code> | </code> | ||