正在使用PowerShell中的WinSCP.NET程序集重命名SFTP服务器上的文件

正在使用PowerShell中的WinSCP.NET程序集重命名SFTP服务器上的文件,powershell,sftp,winscp,winscp-net,Powershell,Sftp,Winscp,Winscp Net,尝试使用PowerShell中的WinSCP.NET程序集将文件从一个文件夹(上载文件夹)移动到另一个文件夹(存档)时出现以下错误: 不能对空值表达式调用方法。 位于C:\Attention Integration\Scripts\Power Shell Script\Download&MoveToArchive.ps1:28 char:5 +$Sftp.RenameRemoteFile($existingFilepath,$newFilepath) + ~~~~~~~~~~~~~~~~

尝试使用PowerShell中的WinSCP.NET程序集将文件从一个文件夹(上载文件夹)移动到另一个文件夹(存档)时出现以下错误:

不能对空值表达式调用方法。
位于C:\Attention Integration\Scripts\Power Shell
Script\Download&MoveToArchive.ps1:28 char:5
+$Sftp.RenameRemoteFile($existingFilepath,$newFilepath)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:InvalidOperation:(:)[],运行时异常
+FullyQualifiedErrorId:InvokeMethodOnFull
以下是我用来传输文件的代码:

# Connect
$session.Open($sessionOptions)

$existingFilepath = "/upload/attendance v2-201709220930.csv"
$newFilepath = "/Archive/attendance v2-201709220930.csv"

# Transfer files
$session.GetFiles($existingFilepath,"C:\Transfer Files\Attendance Files\*").Check()

$Sftp.RenameRemoteFile($existingFilepath,$newFilepath)
您缺少
$Sftp
的分配

使用:


您的代码没有任何意义:

$Sftp.RenameRemoteFile($existingFilepath,$newFilepath)
  • 代码中没有
    $sftp
    变量
  • 没有任何
    RenameRemoteFile
    方法

我在哪里错过了“$Sftp”的分配?我在任何地方都看不到它。我不熟悉sftp,但在调用
RenameRemoteFile
之前,您需要在某个地方分配
$sftp
,我的回答是否有帮助?(我看到你问了一个新问题)嗨@martin,这是我在包含$session.MoveFile时遇到的错误--------你不能对空值表达式调用方法。在C:\Ratnesh\Projects\DCB Bank\Attention Integration\Scripts\Power Shell Script\Download&MoveToArchive.ps1:25 char:5+$session.MoveFile($existingFilepath,$newFilepath)。Check()+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~类别信息:无效操作:(::[]~~~~~,RuntimeException+FullyQualifiedErrorId:InvokeMethodUnull是否在
$session.GetFiles
之后调用
$session.MoveFile
GetFiles
是否工作,并且
MoveFile
是否失败?向我们展示您的完整代码。以下代码有效:`#下载文件$session.GetFiles($existingFilepath,$localpath)。检查()#将文件从上载移动到存档文件夹$session.MoveFile($existingFilepath,$newFilepath)`
$Sftp.RenameRemoteFile($existingFilepath,$newFilepath)