Powershell-变量未扩展

时间:2020-05-15 12:21:09

标签: powershell

在某些服务器中,变量$cygwin_folder可以不同。但是由于某种原因,它并没有扩大。也许是由于斜杠c:\$cygwin_folder引起的,但我不确定如何解决此问题?

c:\$cygwin_folder\bin\bash.exe -c some_command

代码如下:

if(test-path("c:\cygwin")) {
$cygwin_folder = "cygwin";
}
ElseIF (test-path("c:\cygwin64")) {
$cygwin_folder = "cygwin64";
}
Else {
echo "Cygwin directory not found";
exit
}

c:\$cygwin_folder\bin\bash.exe -c some_command

错误:

c:\$cygwin_folder\bin\bash.exe : The term 'c:\$cygwin_folder\bin\bash.exe' is not recognized as the name of a cmdlet, 
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path 
is correct and try again.
At C:\Program Files\zabbix\userparams\myscript.ps1:79 char:1
+ c:\$cygwin_folder\bin\bash.exe -c ("/usr/bin/scp -o StrictHostKeyChecking=no -o  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (c:\$cygwin_folder\bin\bash.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

1 个答案:

答案 0 :(得分:5)

隐式字符串扩展仅适用于参数参数,请使用Promise.all()调用运算符将​​扩展字符串作为命令来调用:

&
相关问题