Topic "Connection has been unexpectedly closed. Server sent command exit status 0, winscp with powershell"

Author Message
tvpatil
[View user's profile]

Joined: 2016-07-12
Posts: 1
Location: india
i am getting below error to automate the synchronization of files remote to local, please help on this error...
error:Connection has been unexpectedly closed. Server sent command exit status 0.

param (
$localPath = "xxxxx",
$remotePath = "xxxxx"
)

try
{# Load WinSCP .NET assembly
Add-Type -Path "C:\WinScp\winscp577automation\WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "xyz.com"
PortNumber=xxxx
UserName = "username"
Password = "password"
SshHostKeyFingerprint = "ssh-rsa 4096 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

# Synchronize files to local directory, collect results
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $False)

# Deliberately not calling $synchronizationResult.Check
# as that would abort our script on any error.
# We will find any error in the loop below
# (note that $synchronizationResult.Downloads is the only operation
# collection of SynchronizationResult that can contain any items,
# as we are not removing nor uploading anything)

# Iterate over every download
foreach ($download in $synchronizationResult.Downloads)
{
# Success or error?
if ($download.Error -eq $Null)
{
Write-Host ("Download of {0} succeeded, removing from source" -f
$download.FileName)
# Download succeeded, remove file from source
$removalResult = $session.RemoveFiles($session.EscapeFileMask($download.FileName))

if ($removalResult.IsSuccess)
{
Write-Host ("Removing of file {0} succeeded" -f
$download.FileName)
}
else
{
Write-Host ("Removing of file {0} failed" -f
$download.FileName)
}
}
else
{
Write-Host ("Download of {0} failed: {1}" -f
$download.FileName, $download.Error.Message)
}
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 25034
Location: Prague, Czechia
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License