Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何从ps1文件运行WinSCP_Powershell_Batch File_Winscp_Windows Task Scheduler - Fatal编程技术网

Powershell 如何从ps1文件运行WinSCP

Powershell 如何从ps1文件运行WinSCP,powershell,batch-file,winscp,windows-task-scheduler,Powershell,Batch File,Winscp,Windows Task Scheduler,我的脚本中有一个.ps1文件。 在这个脚本中,我有一行: Start-Process "C:\activatebatch.bat" 如果我直接用ps1文件执行它,一切都很好。但是如果我设置了一个以ps1作为执行程序的Windows调度程序,bat文件不会启动。在那个bat文件中,我有一个WinSCP,它将文件发送到服务器 如何将其设置为从存储在Windows计划程序中的.ps1启动.bat?或者如何直接从PowerShell代码执行WinSCP 我需要它调用WinSCP将文件发送到服务器-选项

我的脚本中有一个.ps1文件。 在这个脚本中,我有一行:

Start-Process "C:\activatebatch.bat"
如果我直接用ps1文件执行它,一切都很好。但是如果我设置了一个以ps1作为执行程序的Windows调度程序,bat文件不会启动。在那个bat文件中,我有一个WinSCP,它将文件发送到服务器

如何将其设置为从存储在Windows计划程序中的.ps1启动.bat?或者如何直接从PowerShell代码执行WinSCP

我需要它调用WinSCP将文件发送到服务器-选项存储在批处理文件中:

“C:\Program Files(x86)\WinSCP\WinSCP.exe”用户:password@IPI.PIP.IP.IP/命令^
“放置C:\ss\file.txt/home/scripts/windows/”^
“退出”

如果批处理文件中有一个正常工作的WinSCP命令行,则可能需要进行一些更改,以使其与PowerShell兼容:

  • 批处理文件使用^(插入符号)转义新行。PowerShell使用`(倒勾)。因此,将插入符号替换为反勾号
  • 显然,您需要对PowerShell中具有特殊含义的任何字符进行转义,尤其是$(美元符号)、`(反勾号)和
在您的简单脚本中,只有第一点很重要,因此正确的命令是:

& "C:\Program Files (x86)\WinSCP\WinSCP.exe" user:password@IPI.PIP.IP.IP /command `
    "put C:\ss\file.txt /home/scripts/windows/" `
    "exit"

尽管我进一步建议您出于调试目的使用和添加

也不推荐使用命令行参数打开会话。您应该使用(最好还指定协议前缀-
sftp://
?)


WinSCP5.14测试版实际上可以



尽管为了更好地控制,还是建议使用。

您可以尝试使用
启动进程cmd.exe-ArgumentList”/C、“/C:\activatebatch.bat”
,看看是否有效。如果希望cmd窗口在快速完成时保持不变,可以将
/C
替换为
/K
。不,它不工作。在ps1中,它是正常的,就像以前一样,我留下了一个参数选项/K可用,但任何cmd窗口都会显示,并且服务器上的文件没有修改。计划任务是如何配置的?不能将.ps1文件作为要在其中执行的命令。必须将
powershell.exe
作为命令,将
-C:\path\to\your.ps1
作为命令的参数。另外,您可能应该调用
Start Process-Wait
,或者使用call操作符而不是
Start Process
。但是PowerShell脚本中的所有其他内容(无论是什么)都可以工作吗?您是否收到任何错误消息?您是否在任务中使用“开始”以及它设置为什么?WinSCP有一个.NET库。。为什么不使用它而不是调用可执行文件呢?谢谢,现在它工作得很好。如果我通过Winscp.com在PowerShell中执行它,不仅从.exe,我还可以登录到PowerShell控制台。现在我将使用.com扩展来使用WinSCP。Uff,现在我将修改ps1脚本,使其直接从自身发送,而无需批处理文件。非常感谢你。你给了我解决这个问题的好办法。但在来源上,主要问题是奇怪的。
& "C:\Program Files (x86)\WinSCP\WinSCP.exe" /command `
    "open user:password@IPI.PIP.IP.IP" `
    "put C:\ss\file.txt /home/scripts/windows/" `
    "exit"