Why does this array of PSFTP commands not work on some Windows devices

125 Views Asked by At

I have the following snippet of code in a PowerShell script:

    $cmd = @(
       "lcd $local_directory",
       "mget $remote_files",
       "del $remote_files"
    )
$ErrorActionPreference = "SilentlyContinue"
$cmd | & $psftp $sftpserver "-l" $user "-i" $private_key "-v"

This exact same code works on one Windows server and not on another. The only part that does not work is the "lcd" part. When it does work psftp changes the local directory perfectly. When it does not work I can see in the logs that its attempting to change to some portion of the local path I have passed it. For instance if I passed "C:\folder1\documents" it would show it attempted to change to "er1\documents" or some other fraction of the path. I have attempted different directories, different powershell versions, hardcoding the local path. Additionally, if I remove the lcd command I see odd behavior with the mget command. Is there something I am missing regarding passing arguments to cmd from powershell?

EDIT: I have more info, but more confusion. Each time I add another argument I get a piece of the actual local directory back... So with the following code snippet I actually get a properly working lcd argument. but why!!!!

    $cmd = @(
    "lcd 'C:\'",
    "lcd 'C:\'",
    "lcd 'C:\'",
    "lcd $local_directory",
    "mget $remote_files"
    )

EDIT 2:

This apparently has nothing to do with the specific lcd command. I still dont have an answer for this so I'm hoping someone can help. Through further testing I've learned that the $cmd works if I add a 32 character long value to the first index in that array. (Curious that its 32)

So this also works:

$garbage = "                               "
$lcd_command = ("lcd " + $local_directory)
$mget_command = ("mget " + $remote_files)

$cmd = $garbage, $lcd_command, $mget_commandcode

I am at my wits' end here... please help

Thank you

1

There are 1 best solutions below

0
CamParker On

The issue is not specifically the lcd command. I did further troubleshooting and found that for whatever reason I have to send 32 characters of something before sending the actual commands... The above works flawlessly.

$garbage = "                               "
$lcd_command = ("lcd " + $local_directory)
$mget_command = ("mget " + $remote_files)

$cmd = $garbage, $lcd_command, $mget_command

Answer: I was able to reproduce the error/issue on a normally working environment by simply logging in with a non admin account. So, this is somehow a permissions issue. A very odd permissions issue. And, if you do as I mentioned above and pass a 32 character string before the psftp commands it will bypass the permissions issue.

Elaborated Answer: To reproduce my situation try running a powershell script that calls psftp.exe and passes in commands like the ones I've shown in my post. Do this as a Windows user that has standard permissions but not local administrator rights. You should encounter the same issue that the initial lcd command (first index in array of commands sent from powershell) will fail in psftp. Now modify your array of commands to pass a 32 character string as the first index value in the array and your lcd command will suddenly work.