Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

Guest

Re: Working script with "You cannot call a method on a null-valued expression"

martin wrote:

Session.MoveFile does not return anything.

Oh yes. Thank you. I was believing ALL get/put/move calls throw an error to be checked in a similar way... note to self: RTFM... :-)
martin

Re: Working script with "You cannot call a method on a null-valued expression"

Session.MoveFile does not return anything.
https://winscp.net/eng/docs/library_session_movefile

It should be simply:
$session.MoveFile($transfer.FileName, "/somepath/transfered/")
hafo_72

Working script with "You cannot call a method on a null-valued expression"

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 !!!