Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

Barosak

WinSCP script

Hello,

i am using a script below to upload file to SFTP using proxy, but I am getting Time Out error. How can i fix this? Am I connecting first to proxy and doing everything correctly?

Thank you so much.


# Load Assembly .NET WinSCP (DLL in windows\system32)
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

# Set session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
# Protocol, user, password, target server, connection info
Protocol = [WinSCP.Protocol]::Sftp # Declare SFTP protocol
HostName = "hostname"
PortNumber = 22
UserName = "username"
Password = "pass"
GiveUpSecurityAndAcceptAnySshHostKey = $true
Timeout = 300000 # Set timeout to 5 minutes (300000 ms)
}

# Activate WinSCP SFTP session
$session = New-Object WinSCP.Session # WINSCP

# Configure proxy
$session.AddRawConfiguration("ProxyMethod", "3") # 3 = HTTP Proxy
$session.AddRawConfiguration("ProxyHost", "Srv01prox")
$session.AddRawConfiguration("ProxyPort", "80")
$session.AddRawConfiguration("ProxyLoginTimeout", "300000")

# GO
try {
# Open Connection
$session.Open($sessionOptions)

# File Transfer – default over-write
$transferResult = $session.PutFiles("Time_Actual_IKOS.csv", "/TCZW_IKOS_Plant_Cockpit/")

# Log result
$logMessage = "Upload result: $($transferResult.Transfers.Count) file(s) transferred.`n"
foreach ($transfer in $transferResult.Transfers) {
$logMessage += "Transferred: $($transfer.FileName)`n"
}
$logMessage | Tee-Object -FilePath "upload_log.txt" -Append
}
catch {
"Error: $_" | Tee-Object -FilePath "upload_log.txt" -Append
Write-Host "Error: $_"
}
finally {
$session.Dispose()
Read-Host "Press Enter to exit..."
}