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