Re: Unable to see and download files using PowerShell WinSCP .NET DLL
Well, it might work sometimes. But it mostly won't. You cannot replicate everything you can do in GUI in script.
directory tree(GUI can get 'xx.xxx.xxxx.', but script cannot.)
|----\<root>
|-----'xx.xxx.xxxx.'
|-----abc
|-----def
|-----ghi
file panel directory
|----\<root>
|-----abc
|-----def
|-----ghi
MethodInvocationException
errors.
FileExist
method to see what is going on and which confirms this. The method said the same folder the GUI sees doesn't exist. The GUI shows the required directory in the directory tree of the remote server but not in the remote file panel explorer. I don't understand why WinSCP can pick that up and PowerShell with the WinSCP .NET DLL can't. The code is shown below. Any advice or pointers on how to resolve this?
try
{
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "xxx.xxx.xxx.xxx"
PortNumber = xxxx
UserName = "xxxxxxx"
Password = "xxxxxxx" #password as a special character in it
FtpSecure = [WinSCP.FtpSecure]::Explicit
TlsHostCertificateFingerprint = "xx:xx:xx:xx:xx:xx:xx"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Both xyz and 'xx.xxx.xxxx.' are in the same root directory.
# id file is in xyz, and xxx123 is in 'xx.xxx.xxxx.' respectively.
# This script says 'id' exist, but 'xxx123' doesn't. And It also doesn't
# recognize 'xx.xxx.xxxx.', but sees xyz. The folder name of
# 'xx.xxx.xxxx.' actually has quotes and periods as in exactly 'xx.xxx.xxxx.'
#$remotePath = "/xyz/id"
$remotePath = "/'xx.xxx.xxxx.'/xxx123"
if ($session.FileExists($remotePath))
{
Write-Host "File $remotePath exists"
# Now you can e.g. download file using session.GetFiles
exit 0
}
else
{
Write-Host "File $remotePath does not exist"
exit 1
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 2
}
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "xxx.xxx.xxx.xxx"
PortNumber = xx21
UserName = "USERNAME"
Password = "******"
FtpSecure = [WinSCP.FtpSecure]::Explicit
TlsHostCertificateFingerprint = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}
$session = New-Object WinSCP.Session
try
{
#Logging
$session.SessionLogPath = 'C:\Users\xxx\xxx\...\!S!T'
# Connect
$session.Open($sessionOptions)
# Transfer files
$session.GetFiles("/'xx.xxx.xxxx.'/xxx123", "C:\Users\xxxx\xxxxx").Check()
}
finally
{
$session.Dispose()
}
Exception calling "Check" with "0" argument(s): "Can't get attributes of file '/'xx.xxx.xxxx.'/xxx123'.
Could not retrieve directory listing
CWD cmd failed : EDC5129I No such file or directory. (errno2=0x05190050)"
At line:26 char:5
+ $session.GetFiles("/'xx.xxx.xxxx.'/xxx123'", "C:\Users\xxx ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException