gpt4 book ai didi

powershell - 如何从 ps1 文件运行 WinSCP

转载 作者:行者123 更新时间:2023-12-02 23:37:06 25 4
gpt4 key购买 nike

我的脚本有一个 .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" user:password@IPI.PIP.IP.IP /command ^
"put C:\ss\file.txt /home/scripts/windows/" ^
"exit"

最佳答案

如果您在批处理文件中有一个可用的 WinSCP 命令行,您可能需要做一些更改以使其与 PowerShell 兼容:

  • 批处理文件使用 ^(插入符号)转义一个新行。 PowerShell 使用 `(反引号)。因此,请将您的插入符号替换为反引号。
  • 而且您显然需要转义任何在 PowerShell 中具有特殊含义的字符,尤其是 $(美元符号)、`(反引号)和 inner double quotes .

在您的简单脚本中,只有第一点很重要,因此正确的命令是:

& "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 instead of winscp.exe并添加 /log switch to enable session logging用于调试目的。

同时使用命令行参数打开 session 已被弃用。你应该使用 open command (最好还指定一个协议(protocol)前缀 – 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 居然可以generate a PowerShell – WinSCP command template for you .


虽然为了更好的控制,还是推荐使用WinSCP .NET assembly from PowerShell .

关于powershell - 如何从 ps1 文件运行 WinSCP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52852119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com