【问题标题】:How do I run a command from a network path in PowerShell/batch file如何从 PowerShell/批处理文件中的网络路径运行命令
【发布时间】:2017-11-14 00:02:26
【问题描述】:

基本上我要运行的是:

cd /d W:\RemoteMirror\ && winscp.com /ini=nul /script=Remote_Mirror_WinSCP.txt

或者这个:

cd /d \\fileServer\RemoteMirror\ && winscp.com /ini=nul /script=Remote_Mirror_WinSCP.txt

当然,对于 SQL Server,我不能使用 UNC 路径。但我不知道该怎么做。我对 PowerShell 还不太熟悉,但这是我最好的猜测。

【问题讨论】:

  • 你能运行一个批处理文件吗?
  • 您的命令行以cd 开头。您只是尝试更改默认文件夹还是尝试做其他事情?
  • 使用PUSHDPOPD。如果您指定 UNC 名称,它将分配一个未使用的驱动器号。使用PUSHD /?阅读所有相关信息。
  • 你说的“对于 SQL Server,我不能使用 UNC 路径”是什么意思
  • @maSTAShuFu 我认为问题在于 SQL Server 服务帐户无法以完全权限运行。

标签: sql-server powershell cmd sql-server-2012 winscp


【解决方案1】:

UNC 路径不能是当前工作目录(这是 Windows 的限制,与 SQL Server 无关)。


正如@lit 已经评论的那样,您可以使用pushd command 为UNC 路径临时分配一个驱动器号并立即将工作目录更改为该路径。


或者,修改您的 WinSCP 脚本,使其不依赖于当前工作目录。

只需在脚本中使用完整的 UNC 路径,例如:

get "/remote/path/*" "\\fileServer\RemoteMirror\"

另见Using batch file and WinSCP to download files from the FTP server to file server (shared folder)


或者,如果您需要让脚本使用不同的目录,请使用parameterized script

get "/remote/path/*" "%1%\"

然后像这样运行它:

winscp.com /ini=nul /script=Remote_Mirror_WinSCP.txt /parameter \\fileServer\RemoteMirror

查看使用parameterized scripts的完整示例。

【讨论】:

  • 我现在可以运行脚本,但文件没有移动,并且该步骤似乎无限运行,就好像提示窗口正在等待用户输入一样。有没有办法在将 cmd 窗口作为计划任务运行时显示它? @martin-prikryl
  • 使用/log=“C:\path\log.log”命令行开关启用日志记录。
  • 所以将winscp.com 的输出重定向到一个文件:winscp.com ... > c:\path\winscp.logPATH 中还有 winscp.com 吗?如果您手动执行脚本,它甚至可以工作吗?
  • 手动执行时确实有效,手动运行时会记录。它位于 PATH 变量中。这就是我在文件中被调用的内容:# Connect open sftp://xxx:xxx@xxx.com/ -hostkey="ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" # Force binary mode transfer option transfer binary # Download file to the local directory synchronize -filemask="*.pgp|*/" local %1% / # Disconnect close # Exit WinSCP exit
  • cmd.exe /k "winscp.com /ini=nul /log="C:\tools\log.log" /script=C:\tools\Mirror_WinSCP.txt /parameter \\fs1\dataprocessing\RemoteMirror 这就是我在 SQL 任务中的内容。我刚刚意识到你是 WinSCP 的开发者。非常感谢你帮我解决这个问题!
猜你喜欢
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 2020-09-10
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
相关资源
最近更新 更多