2017-10-11 60 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' –