Need Help - this should be an easy one

I have a WinSCP session thru the UI, I copied the PS code from the main window.

I am trying to have the file that gets downloaded to automatically get copied to another drive as part of the download. It downloads on C:\Data and I want it copied to D:\Data
Ultimately I will create a daily Windows task to perform the daily download via a PowerShell.

My environment is Windows Server , no Linix/Unix

The existing code is :

Needed for PowerShell 7.3 and newer

$PSNativeCommandArgumentPassing = “Legacy”

& “C:\Program Files (x86)\WinSCP\WinSCP.com” /log="C:\writable\path\to\log\WinSCP.log" /ini=nul
/command "open ftps://dins%2Fsa_automation%40.com:<password>@company.sharefileftp.com/"
“Your command 1” "Your command 2"
“exit”

$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
Write-Host “Success”
}
else
{
Write-Host “Error”
}

exit $winscpResult

Hello, please edit your post and format your code:
https://forums.powershell.org/t/welcome-to-the-new-powershell-forums/7/4
You’re looking for the “preformatted text” button on the toolbar.
Or, you can put a line with 3x backtick characters before and after your code.

You also didn’t explain what isn’t working with your current code. Is there an error message?

Thanks for the reply. No error message received, I am trying to add an additional “copy” step to the download process. I will pull down the code in the correct format. I was not sure the best way to pose the question as I am not a programmer.

Hopefully this is what you asked for, this code is the output when I establish a SFTP connection to the remote host using Win SCP. I want to pull down a single file, the only one in the directory, and copy to a different drive on the server. I want to save the code as a powershell .ps and then execute it daily as a Windows scheduler job.

@echo off

“C:\Program Files (x86)\WinSCP\WinSCP.com” ^
/log=“C:\writable\path\to\log\WinSCP.log” /ini=nul ^
/command ^
“open ftps://dins%2Fsa_automation%%40company.com:@company.sharefileftp.com/” ^
“Your command 1” ^
“Your command 2” ^
“exit”

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)

exit /b %WINSCP_RESULT%

It’s not what I asked for no. I asked for your share code to be “formatted” in a readable way.
Go back and click the link I provided which has a demo video on how to format code.
Edit your post(s), select the text representing code then find the “preformatted text” button on the toolbar to format everything.

@echo off

“C:\Program Files (x86)\WinSCP\WinSCP.com” ^
/log=“C:\writable\path\to\log\WinSCP.log” /ini=nul ^
/command ^
“open ftps://dins%2Fsa_automation%%40company.com:@company.sharefileftp.com/” ^
“Your command 1” ^
“Your command 2” ^
“exit”

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)

exit /b %WINSCP_RESULT%

The end result should look something like that. Though this isn’t Powershell, it’s CMD.

“C:\Program Files (x86)\WinSCP\WinSCP.exe” ^
/log=“C:\Temp\winscp.log” /command ^
“open sftp:///sa_automation@.com:@://.sharefileftp.com `”"
“get /remote/path/file.txt C:\DATA” “exit”

I’m sorry, but you’re still not formatting the code as code.
here I’ve formatted your most recent example:

"C:\Program Files (x86)\WinSCP\WinSCP.exe" ^
/log="C:\Temp\winscp.log" /command ^
"open sftp:///sa_automation@.com:@://.sharefileftp.com `""
"get /remote/path/file.txt C:\DATA" "exit"

I replaced all of the double quote characters with the correct ASCII characters but otherwise this is unmodified CMD code. This is not PowerShell.
Really it seems like the bulk of what you’re trying to accomplish is done within the CLI use of WinSCP anyway. This does not seem like a PowerShell forum issue.
You might check our the WinSCP Forum or maybe just review the documentation for command line usage.

Thanks. Appreciate the help. I’ll try the WinSCP page.

How are you controlling where the downloaded file will be stored? I don’t see a “lcd C:\DATA” in your “command”, nor is there a “get” command.

Having the complete data code is important, But to offer a best guess, I think all you need is a Copy-File cmdlet after the 'Write-Host ‘Success’".

Thanks for the comment. That was the original request.

How do I add a “get” command to change into the directory C:/Data.

For some reason I cannot export the code as the other person requested. I can “generate session URL/Code” from the UI, not in the format requested.

Give this a try. I haven’t run it, but I think I have the quoting done correctly.

$PSNativeCommandArgumentPassing = "Legacy"

$DownLoadFile    = 'RemoteFile.txt' # assumes the file live in the FTP login directory
                                    # if it doesn't, provide the necessary path to the
                                    # file
$TargetDirectory = 'Data'
$TargetDrive     = "C"
$CopyDrive       = "D"
$TargetName      = "{0}:\{1}\{2}" -f $TargetDrive,$TargetDirectory,$DownLoadFile
$CopyName        = "{0}:\{1}\{2}" -f $CopyDrive,$TargetDirectory,$DownLoadFile

& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
/log="C:\writable\path\to\log\WinSCP.log" `
/ini=nul `
/command `
    "open ftps://dins%2Fsa_automation%40.com:<password>@company.sharefileftp.com/" `
    "get `"$DownLoadFile`" `"$TargetName`"" `
    "exit"

$winscpResult = $LastExitCode
if ($winscpResult -ne 0)
{
    Write-Host "WinSCP transfer failed. Exit code = $winscpResult"
}
elseif (-not (Test-Path $TargetName))
{
    Write-Host "WinSCP reported success but file not found: $TargetName"
}
else
{
    Write-Host "Success"
    Copy-Item -Path $TargetName -Destination $CopyName
}
exit $winscpResult

Thank you. I will try it in the morning when I am back at my desk.

I’ll post the results.

To post preformatted text, look at the icons either at the top, or bottom, of the post. Click the one that looks like a circle with a “+” in the middle and select the choice that says “</> Preformatted text”. That should cause three lines to appear in your post. The first line is three back-ticks. The second line is “type or paste code here”. The third line is three back-ticks. Put your code between the lines of back-ticks.

you dont need a separate command to change into C:\Data on the local side, get takes it as the second argument, so get RemoteFile.txt C:\Data\ should drop it there. cd is only for the remote folder, and lcd would be the local equivalent if you wanted to set the working folder first.