我需要在运行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。
-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.
想通了。当使用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]:~/")