如何从PS1文件运行WinSCP [英] How to run a WinSCP from ps1 file
问题描述
我的脚本中有一个.ps1文件.在此脚本中,我有一行:
I have a .ps1 file with my script. In this script I have a line with:
Start-Process "C:\activatebatch.bat"
如果我直接使用ps1文件执行它,则一切正常.但是,如果我将Windows Scheduler设置为ps1作为执行程序,则bat文件不会启动.在该bat文件中,我有一个WinSCP,它将文件发送到服务器.
If i execute it directly with ps1 file, everything works well. But if I set up a Windows Scheduler with ps1 as executive, the bat file doesn't start. In that bat file I have a WinSCP which sends file to server.
我如何设置它从Windows Scheduler中存储的.ps1启动.bat?或者如何直接从PowerShell代码执行WinSCP?
How could I set it to start .bat from .ps1 stored in Windows Scheduler? Or how could I execute WinSCP directly from PowerShell code?
我需要它来调用WinSCP将文件发送到服务器-并且选项存储在批处理文件中:
I need it to call for WinSCP to send a file into server - and the options are stored in batch file:
"C:\Program Files (x86)\WinSCP\WinSCP.exe" user:password@IPI.PIP.IP.IP /command ^
"put C:\ss\file.txt /home/scripts/windows/" ^
"exit"
推荐答案
如果批处理文件中有运行的WinSCP命令行,则可能需要进行一些更改以使其与PowerShell兼容:
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:
- 批处理文件使用^(脱字符号)转义新行.PowerShell使用`(反引号).因此,将您的插入符号替换为反引号.
- 您显然需要在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"
尽管我会进一步建议您使用
winscp.com
代替winscp.exe
并添加/log
切换以启用会话日志记录以进行调试.
Though I'd further recommend you using
winscp.com
instead ofwinscp.exe
and adding/log
switch to enable session logging for debugging purposes.也不建议使用命令行参数打开会话.您应该使用
open
命令(最好还指定协议前缀-sftp://
?).Also opening a session using a command-line argument is deprecated. You should use
open
command (and better also specify a protocol prefix -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实际上可以为您生成PowerShell-WinSCP命令模板.
尽管为了更好地控制,建议使用 PowerShell中的WinSCP .NET程序集.
Though for a better control, it's recommended to use WinSCP .NET assembly from PowerShell.
这篇关于如何从PS1文件运行WinSCP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!