I have some strange behaviour on using GetFiles() to download *.xml files from remote to local (working) and then, foreach $transferResult.Transfers, MoveFile() to a remote subfolder (also working).
But with the $moveResult.Check() right after the MoveFile(), it throws this "You cannot call a method on a null-valued expression", but moved the file as expected.
Testing with more then 1 file, this also works, when removing the check so that it does not step out of the foreach...
Could anybody tell me what this error is about?
I only found similar cases where someone tried to to the .Check() far outside an if or somehow NOT right after the function all, what makes sense not to work. But I can't see what is wrong here...
Code example:
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.GetFiles("/somepath/*.xml", $destpath, $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# move copied files and print results
foreach ($transfer in $transferResult.Transfers)
{
#print
$text = $logdate + " Download of $($transfer.FileName) done"
$text >> $logfile
#move
$moveResult = $session.MoveFile($transfer.FileName, "/somepath/transfered/")
# throw on any error
# WHY DOES THIS THROW a "You cannot call a method on a null-valued expression" ERROR ???
$moveResult.Check()
#print
$text = $logdate + " File $($transfer.FileName) moved."
$text >> $logfile
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch [Exception]
{
$text = $logdate + " Error: $($_.Exception.Message)"
$text >> $logfile
exit 1
}
Thanks in advance !!!