1

I've got a batch script which runs via schtasks every night on different remote Windows PCs (Windows 7 32-bit SP1). These PCs need to FTP files to and from a Linux server. Currently using FTP, the batch script works just fine.

Now the Linux servers are being changed over to use only SFTP. I'd like to write a short PowerShell script to SFTP the same files to/from the server – I've got Posh-Ssh installed to use in PowerShell on these Windows systems (PS Version 5.1).

Currently the part of the batch script doing the FTP looks like this:

REM make temp file for FTP
echo open pos%STORE%               >  C:\temp\temp.txt
echo username                      >> C:\temp\temp.txt
echo password                      >> C:\temp\temp.txt
echo ascii                         >> C:\temp\temp.txt    
echo cd /path/to/use               >> C:\temp\temp.txt
echo put %USERDOMAIN%.ftp          >> C:\temp\temp.txt
echo put posdaily.txt posdaily.ftp >> C:\temp\temp.txt
echo get prod.txt                  >> C:\temp\temp.txt
echo get posdaily.ftp              >> C:\temp\temp.txt
echo quit                          >> C:\temp\temp.txt

REM **** FTP to put/get files from POS Server ****
echo First Try Connect to POS ....
ftp -iv -s:\temp\temp.txt >> C:\log\Daily.log  2>&1

Is there a way to use this same temp.txt that's being created to pass the username/password/put/get to a PowerShell script that uses Posh-Ssh? What changes would I need to make to the temp.txt file?

I can call a separate batch script which runs the PowerShell script – I've done that in previous batch scripts – but I'm not sure how to write the PowerShell script – what command/commands would I use in the PowerShell script to use the data in temp.txt?

If I get it working in PowerShell, I should be able to put a call to this same PowerShell script in a C# and VB script that need to do that same thing.

0

1 Answer 1

3

Is there a way to use this same temp.txt that's being created to pass the username/password/put/get to a Powershell script that uses Posh-Ssh?

No you cannot. Posh-Ssh is a native PowerShell module, which does not consume scripts like ftp does.

Also as @SomethingDark already commented, there's no need to switch to PowerShell for SFTP. Not even for C#/VB.NET. What is the difference between calling a PowerShell script and a batch file from C#/VB.NET?

For SFTP in a batch file, see:
Secure FTP using Windows batch script

My answer there suggest using (my) WinSCP SFTP client. And WinSCP can actually consume a script similar (while not identical) to ftp. And there is a guide for converting ftp script to WinSCP script.


Though if you aim to have a same/similar code for PowerShell/C#/VB.NET, you better use an SFTP .NET assembly. See SFTP Libraries for .NET.

If you want a native .NET assembly, I would recommend SSH.NET (though its development ceased in last few years). And you actually use that already, don't you?

WinSCP also has .NET assembly – but it's not a completely native .NET assembly. On the other hand, WinSCP GUI can generate a code template for SFTP for all of PowerShell, C#, and VB.NET.

Though why don't you implement your transfer functionality into a custom assembly and reuse it from all of PowerShell/C#/VB.NET?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.