Powershell-使用CYGWIN运行SCP命令,还扩展变量

问题描述 投票:0回答:1

我需要在运行SCP命令之前扩展变量,因此我不能使用单引号。如果我在Powershell ISE中使用双引号运行脚本,则可以正常工作。

但是如果我通过命令提示符运行脚本,则不起作用。

我正在使用zabbix来运行脚本,该脚本将脚本称为[cmd /C "powershell -NoProfile -ExecutionPolicy Bypass -File .\myscript.ps1"]

这是需要使用Cygwin bash运行SCP的代码。

if ((test-path  "$zipFile"))

{ 

C:\cygwin\bin\bash.exe -l "set -x; scp /cygdrive/e/logs/$foldername/dir1/$foldername.zip [email protected]:~/"

}

输出:

/usr/bin/bash: set -x;  /cygdrive/e/logs/myfolder/dir1/server.zip [email protected]:~/: No such file or directory

如果我在Cygwin中手动运行与上面相同的命令,它将起作用。

我什至试图使用bash -l -c,但随后SSH会话被卡住了,可能是因为[email protected]根据文档变为$ 1。

Documentation link

   -c        If the -c option is present, then commands are read from

             the first non-option argument command_string.  If there are

             arguments after the command_string, the first argument is

             assigned to $0 and any remaining arguments are assigned to

             the positional parameters.  The assignment to $0 sets the

             name of the shell, which is used in warning and error

             messages.
powershell cygwin scp
1个回答
0
投票

想通了。当使用bash -c时,由于已知的主机事物StrictHostKeyChecking,系统停止了运行(提示您键入yes / no)。我将-v开关设置为SCP,它向我显示了停止的调试日志。

必须设置scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null选项。

现在完整的行如下所示:

c:\$cygwin_folder\bin\bash.exe -c ("/usr/bin/scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -v -i /cygdrive/c/cygwin/home/myuser/.ssh/id_rsa  /cygdrive/e/logs/$foldername/dir1/$foldername.zip [email protected]:~/")
© www.soinside.com 2019 - 2024. All rights reserved.