[英]How to run a WinSCP from ps1 file
I have a .ps1 file with my script.我的脚本有一个 .ps1 文件。 In this script I have a line with:在这个脚本中,我有一行:
Start-Process "C:\activatebatch.bat"
If i execute it directly with ps1 file, everything works well.如果我直接用 ps1 文件执行它,一切正常。 But if I set up a Windows Scheduler with ps1 as executive, the bat file doesn't start.但是如果我设置一个以 ps1 作为执行程序的 Windows 调度程序,bat 文件就不会启动。 In that bat file I have a WinSCP which sends file to server.在那个 bat 文件中,我有一个将文件发送到服务器的 WinSCP。
How could I set it to start .bat from .ps1 stored in Windows Scheduler?如何将其设置为从 Windows 调度程序中存储的 .ps1 启动 .bat? Or how could I execute WinSCP directly from PowerShell code?或者如何直接从 PowerShell 代码执行 WinSCP?
I need it to call for WinSCP to send a file into server - and the options are stored in batch file:我需要它来调用 WinSCP 将文件发送到服务器 - 并且选项存储在批处理文件中:
"C:\Program Files (x86)\WinSCP\WinSCP.exe" user:password@IPI.PIP.IP.IP /command ^
"put C:\ss\file.txt /home/scripts/windows/" ^
"exit"
If you have a working WinSCP command line in a batch file, you may need to do few changes to make it compatible with PowerShell:如果批处理文件中有一个可用的 WinSCP 命令行,则可能需要做一些更改以使其与 PowerShell 兼容:
In your simple script, only the first point matters, so the correct command is:在您的简单脚本中,只有第一点很重要,因此正确的命令是:
& "C:\Program Files (x86)\WinSCP\WinSCP.exe" user:password@IPI.PIP.IP.IP /command `
"put C:\ss\file.txt /home/scripts/windows/" `
"exit"
Though I'd further recommend you using winscp.com
instead of winscp.exe
and adding /log
switch to enable session logging for debugging purposes.尽管我进一步建议您使用winscp.com
而不是winscp.exe
并添加/log
开关以启用会话日志以进行调试。
Also opening a session using a command-line argument is deprecated.此外,不推荐使用命令行参数打开会话。 You should use open
command (and better also specify a protocol prefix - sftp://
?).您应该使用open
命令(最好还指定一个协议前缀 - sftp://
?)。
& "C:\Program Files (x86)\WinSCP\WinSCP.exe" /command `
"open user:password@IPI.PIP.IP.IP" `
"put C:\ss\file.txt /home/scripts/windows/" `
"exit"
WinSCP 5.14 beta can actually generate a PowerShell - WinSCP command template for you . WinSCP 5.14 beta 实际上可以为您生成一个 PowerShell-WinSCP 命令模板。
Though for a better control, it's recommended to use WinSCP .NET assembly from PowerShell .虽然为了更好的控制,建议使用PowerShell 中的 WinSCP .NET 程序集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.