尝试将文件从一个文件夹(上传文件夹)移动到另一个文件夹(存档)时出现以下错误,该文件夹在PowerShell中使用WinSCP .NET程序集在SFTP服务器上可用:
You cannot call a method on a null-valued expression.
At C:Attendance IntegrationScriptsPower Shell
ScriptDownload&MoveToArchive.ps1:28 char:5
+ $Sftp.RenameRemoteFile($existingFilepath,$newFilepath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
以下是我用来传输文件的代码:
# Connect
$session.Open($sessionOptions)
$existingFilepath = "/upload/attendance v2-201709220930.csv"
$newFilepath = "/Archive/attendance v2-201709220930.csv"
# Transfer files
$session.GetFiles($existingFilepath,"C:Transfer FilesAttendance Files*").Check()
$Sftp.RenameRemoteFile($existingFilepath,$newFilepath)
使用Session.MoveFile
方法:
$session.MoveFile($existingFilepath, $newFilepath)
您的代码没有任何意义:
$Sftp.RenameRemoteFile($existingFilepath,$newFilepath)
- 代码中没有
$sftp
变量 - WinSCP .NET 程序集没有任何
RenameRemoteFile
方法。
您缺少$Sftp
的分配。