2017-10-11 86 views
2

重命名SFTP服务器上的文件,WinSCP赋予.NET程序集以下错误来当试图将文件从一个文件夹(文件夹上传)移动到另一个(存档),这是可以使用WinSCP赋予.NET程序集在PowerShell中SFTP服务器上:在PowerShell中

You cannot call a method on a null-valued expression. 
At C:\Attendance Integration\Scripts\Power Shell 
Script\Download&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 Files\Attendance Files\*").Check() 

$Sftp.RenameRemoteFile($existingFilepath,$newFilepath) 
+0

难道我的回答帮助? (因为我看到你问了一个新问题) –

回答

1

使用Session.MoveFile method

$session.MoveFile($existingFilepath, $newFilepath) 

您的代码没有任何意义:

$Sftp.RenameRemoteFile($existingFilepath,$newFilepath) 
  • 有没有在你的代码中没有$sftp变量
  • WinSCP .NET assembly没有任何RenameRemoteFile方法。
+0

嗨@马丁在什么地方,这是当我包括$ session.MoveFile我正在错误-------- ---你无法调用一个空值表达式的方法。 在C:\ Ratnesh \ Projects \ DCB-Bank \ Attendance Integration \ Scripts \ Power Shell Script \ Download&MoveToArchive.ps1:25 char:5 + $ session.MoveFile($ existingFilepath,$ newFilepath).Check() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~ + CategoryInfo:InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:InvokeMethodOnNull –

+0

您是否在'$ session.GetFiles'之后调用'$ session.MoveFile'? 'GetFiles'工作和'MoveFile'失败?向我们展示您的完整代码。 –

+0

下面的代码工作:'#下载文件 $ session.GetFiles($ existingFilepath,$的localPath)。检查() #移动文件从上传到归档文件夹 $ session.MoveFile($ existingFilepath,$ newFilepath)' –

0

你缺少分配$Sftp

+0

我在哪里错过了'$ Sftp'的任务? –

+0

我没看到它在任何地方。林不熟悉,SFTP,但你需要指定'$ Sftp'可以调用'RenameRemoteFile' –