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
}
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
}