Topic "SFTP connection works in command line but it doesn't work in Powershelll"

Author Message
bhuro
[View user's profile]

Joined: 2016-06-07
Posts: 3
Location: USA
Hi,

I am using a PowerShell version 4 to creating automating script to download the file from client SFTP.

Here is script in Powershell.I did check the SFTP connection using the same credentails in commandline and it works but when i execute following script in PowerShell ISE(Using administrator) it throwing me error (Error: Connection has been unexpectedly closed. Server sent command exit status 0.). Can someone help me to debug following code ?

try
{
# Load WinSCP .NET assembly
Add-Type -Path "c:\Program Files (x86)\WinSCP\WinSCPNet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "***********"
UserName = "***********"
Password = "***********"
SshHostKeyFingerprint = "*************"
}

$session = New-Object WinSCP.Session

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

# Force binary mode transfer
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

# Download file to the local directory d:\
# Note use of absolute path
$transferResult = $session.GetFiles("//******.Zip", "C:\users\Downloads", $False, $transferOptions)

# Throw on any error to emulate the default "option batch abort"
$transferResult.Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
Advertisements
bhuro
[View user's profile]

Joined: 2016-06-07
Posts: 3
Location: USA
Is there anything that i missing in configuration ?
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Please attach a full session log file from both sessions (working and failing).

To generate 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.
bhuro
[View user's profile]

Joined: 2016-06-07
Posts: 3
Location: USA
prikryl wrote:
Please attach a full session log file from both sessions (working and failing).

To generate 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.


Thank you for quick response. I was able to debug the issue. There was issue with Password which i have double quoted instead of single quote.

Thanks
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