2
votes

Following error is coming when trying to move the file from one folder (upload folder) to another (archive) which is available on SFTP server using WinSCP .NET assembly in 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

Following is the code which I am using to transfer the file:

# 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)
2
Did my answer help? (as I see you have asked a new question)Martin Prikryl

2 Answers

0
votes

Use Session.MoveFile method:

$session.MoveFile($existingFilepath, $newFilepath)

Your code does not make any sense:

$Sftp.RenameRemoteFile($existingFilepath,$newFilepath)
  • There's no $sftp variable in your code
  • WinSCP .NET assembly does not have any RenameRemoteFile method.
0
votes

You are missing the assignment of $Sftp.