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

Advertisement

bhuro
Joined:
Posts:
3
Location:
USA

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

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
}

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

Re: SFTP connection works in command line but it doesn't work in Powershelll

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.

Reply with quote

bhuro
Joined:
Posts:
3
Location:
USA

Re: SFTP connection works in command line but it doesn't work in Powershelll

martin 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

Reply with quote

Advertisement

You can post new topics in this forum