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

Advertisement

hafo_72
Joined:
Posts:
1

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

Reply with quote

Advertisement

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... :-)

Reply with quote

Advertisement

You can post new topics in this forum