简体   繁体   English

如何从 ps1 文件运行 WinSCP

[英]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 兼容:

  • Batch file use ^ (caret) to escape a new line.批处理文件使用 ^(插入符号)来转义新行。 PowerShell uses ` (backtick). PowerShell 使用 `(反引号)。 So replace your carets to backticks.因此,将您的插入符号替换为反引号。
  • And you obviously need to escape any characters with a special meaning in PowerShell, particularly $ (dollar sign), ` (backtick) and inner double quotes .并且您显然需要在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM