Permission denied using MoveFile method

Advertisement

johnmcp
Joined:
Posts:
3
Location:
Ireland

Permission denied using MoveFile method

I have created a simple Powershell script to move files from one location to another on the same server.
This is my code:
$session.Open($sessionOptions)

        #$files = $session.ListDirectory($remotePath)

        # Iterate over every transfer
        $fileInfos = 
            $session.EnumerateRemoteFiles(
                $fromPath, 
                $Null, 
                [WinSCP.EnumerationOptions]::EnumerateDirectories -bor
                    [WinSCP.EnumerationOptions]::AllDirectories)

        foreach ($fileInfo in $fileInfos)
        {           
 
            if ($fileInfo.IsDirectory)
            {
                #TODO
                
            }
            else
            {
                $session.MoveFile($fileInfo, $toPath)
            }
        }

On executing the code I get a Permission denied error.
I have verified the user has the required permissions by performing the same move operation from the WinSCP GUI, so I don't know why I am getting this error when using the script.
Grateful if someone can provide advice on this.

Reply with quote

Advertisement

johnmcp
Joined:
Posts:
3
Location:
Ireland

The documentation also says this:
The name must be included or the target page has to end with a slash

I have set the value of $toPath to this:
$toPath = "/DataWarehouse/Dev/"
Noting that the trailing slash is included.

The exact error description is:
"Error moving file 'AMPS_Transactions.xlsx' to '/DataWarehouse/Dev/AMPS_Tr
ansactions.xlsx'.

Which would indicate to me that the $toPath variable is correct.[/quote]

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,390
Location:
Prague, Czechia

So that's good.
I can see the real problem now:
You do not pass a path to the source file. You pass $fileInfo object (what converts to a mere filename, by an implicit call to .ToString).
Use $fileInfo.FullName.

Reply with quote

Advertisement

You can post new topics in this forum