在批处理脚本中使用 Powershell 的 SFTP
SFTP with Powershell in a batch script
我有一个批处理脚本,每晚通过 schtasks
在不同的远程 Windows PC(Windows 7 32 位 SP1)上运行。这些 PC 需要 FTP 文件往返于 Linux 服务器。当前使用 FTP,批处理脚本工作正常。
现在 Linux 服务器正在转换为仅使用 SFTP。我想写一个简短的 PowerShell 脚本到 SFTP 服务器的相同文件 to/from – 我已经安装了 Posh-Ssh 以便在这些 Windows 系统上的 PowerShell 中使用(PS 版本 5.1).
目前执行 FTP 的批处理脚本部分如下所示:
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
有没有办法使用正在创建的 temp.txt
将 username/password/put/get 传递给使用 Posh-Ssh 的 PowerShell 脚本?我需要对 temp.txt
文件进行哪些更改?
我可以调用一个单独的批处理脚本来运行 PowerShell 脚本——我在以前的批处理脚本中已经这样做了——但我不确定如何编写 PowerShell 脚本——我会使用什么 command/commands在 PowerShell 脚本中使用 temp.txt
中的数据?
如果我让它在 PowerShell 中工作,我应该能够在 C# 和需要执行相同操作的 VB 脚本中调用相同的 PowerShell 脚本。
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?
不,你不能。 Posh-Ssh 是一个原生的 PowerShell 模块,它不像 ftp
那样使用脚本。
此外,@SomethingDark 已经评论过,没有必要为 SFTP 切换到 PowerShell。甚至对于 C#/VB.NET 也不行。 从 C#/VB.NET 调用 PowerShell 脚本和批处理文件有什么区别?
对于批处理文件中的 SFTP,请参阅:
Secure FTP using Windows batch script
我的回答建议使用 (my) WinSCP SFTP client。
WinSCP 实际上可以使用类似于(但不完全相同)ftp
的脚本。还有一个guide for converting ftp
script to WinSCP script.
虽然如果您的目标是为 PowerShell/C#/VB.NET 提供 same/similar 代码,您最好使用 SFTP .NET 程序集。参见 SFTP Libraries for .NET。
如果您想要原生 .NET 程序集,我会推荐 SSH.NET (though its development ceased in last few years). And you actually use that already, don't you?
WinSCP 也 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。
虽然为什么不将传输功能实现到自定义程序集中并在所有 PowerShell/C#/VB.NET?
我有一个批处理脚本,每晚通过 schtasks
在不同的远程 Windows PC(Windows 7 32 位 SP1)上运行。这些 PC 需要 FTP 文件往返于 Linux 服务器。当前使用 FTP,批处理脚本工作正常。
现在 Linux 服务器正在转换为仅使用 SFTP。我想写一个简短的 PowerShell 脚本到 SFTP 服务器的相同文件 to/from – 我已经安装了 Posh-Ssh 以便在这些 Windows 系统上的 PowerShell 中使用(PS 版本 5.1).
目前执行 FTP 的批处理脚本部分如下所示:
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
有没有办法使用正在创建的 temp.txt
将 username/password/put/get 传递给使用 Posh-Ssh 的 PowerShell 脚本?我需要对 temp.txt
文件进行哪些更改?
我可以调用一个单独的批处理脚本来运行 PowerShell 脚本——我在以前的批处理脚本中已经这样做了——但我不确定如何编写 PowerShell 脚本——我会使用什么 command/commands在 PowerShell 脚本中使用 temp.txt
中的数据?
如果我让它在 PowerShell 中工作,我应该能够在 C# 和需要执行相同操作的 VB 脚本中调用相同的 PowerShell 脚本。
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?
不,你不能。 Posh-Ssh 是一个原生的 PowerShell 模块,它不像 ftp
那样使用脚本。
此外,@SomethingDark 已经评论过,没有必要为 SFTP 切换到 PowerShell。甚至对于 C#/VB.NET 也不行。 从 C#/VB.NET 调用 PowerShell 脚本和批处理文件有什么区别?
对于批处理文件中的 SFTP,请参阅:
Secure FTP using Windows batch script
我的回答建议使用 (my) WinSCP SFTP client。
WinSCP 实际上可以使用类似于(但不完全相同)ftp
的脚本。还有一个guide for converting ftp
script to WinSCP script.
虽然如果您的目标是为 PowerShell/C#/VB.NET 提供 same/similar 代码,您最好使用 SFTP .NET 程序集。参见 SFTP Libraries for .NET。
如果您想要原生 .NET 程序集,我会推荐 SSH.NET (though its development ceased in last few years). And you actually use that already, don't you?
WinSCP 也 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。
虽然为什么不将传输功能实现到自定义程序集中并在所有 PowerShell/C#/VB.NET?