连接将无法打开或使用WinSCP将远程FTP文件移动到同一服务器上的子目录中的文件

时间:2019-07-02 16:37:48

标签: powershell winscp-net

Add-Type -Path "WinSCPnet.dll"

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "ftp://ftp.example.com/"
UserName = "username"
Password = "password"
FtpSecure = [WinSCP.FtpSecure]::Explicit
TlsHostCertificateFingerprint = "a6:c3:e8:ce:22:c3:39:27:0d:00:ac:63:cf:78:6d:45:fd:c0:29:b0"
}


$session = New-Object WinSCP.Session



$session.Open($sessionOptions)

$source = '/incoming'
$dest = '/incoming/History'

$files = $session.EnumerateRemoteFiles(
$source, $Null, [WinSCP.EnumerationOptions]::MatchDirectories)
foreach ($file in $files)
{

$session.Open($sessionOptions)
Write-Host "Renaming $file..."
$session.MoveFile($file.FullName, [WinSCP.RemotePath]::CombinePaths($dest, $file.Name))
}

finally
{
$session.Dispose()
}

以下错误:

"Connection failed.
Connection failed."
At line:17 char:1
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException

Exception calling "EnumerateRemoteFiles" with "3" argument(s): "Session is not
opened"
At line:22 char:1
+ $files = $session.EnumerateRemoteFiles(
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException

Exception calling "Open" with "1" argument(s): "Connection failed.
Connection failed."
At line:27 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SessionRemoteException

我想使用WinSCP重命名将远程文件从FTP文件夹移动到同一FTP的子目录中。我在该服务器上使用的所有其他进程都可以与WebClientRequest一起很好地工作,但是由于某些原因,我在将WinSCP模块与之配合使用时遇到了麻烦。

0 个答案:

没有答案