[英]How to propagate windows environment variable for powershell run from cygwin
I have installed cygwin and I am about to execute a script written in powershell. 我已经安装了cygwin,并且即将执行以powershell编写的脚本。 I do it like this:
我这样做是这样的:
powershell "& ""C:\my\script.ps1"""
This works as expected, I have to do this that way because in that script I am executing another external command and so on ... 这按预期工作,我必须那样做,因为在该脚本中我正在执行另一个外部命令,依此类推...
I would like to add some environment variable to that script, so I would like to write something like 我想在该脚本中添加一些环境变量,所以我想写一些类似的东西
powershell "$FOO="bar"; & ""C:\my\script.ps1"""
so I can then access $FOO
variable in the script and do something with it. 这样我就可以在脚本中访问
$FOO
变量并对其进行处理。 The idea is that if that variable is not defined, I use some default value. 想法是,如果未定义该变量,则使用一些默认值。 I know that this could be also achieved with some environment variables or I could put these variables to the profile (profile.ps1) but I want to get rid of that file so I need none and I can override the default value with the variables as I showed.
我知道这也可以通过一些环境变量来实现,或者可以将这些变量放到配置文件(profile.ps1)中,但是我想摆脱该文件,所以我不需要任何文件,并且可以使用以下变量覆盖默认值:我展示出。
but is says that: 但说:
=bar : The term '=bar' 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 line:1 char:1
So I was thinking that something like this could work: 所以我在想这样的事情可能会起作用:
powershell { $web = "google.com" ; & ping.exe $web }
But it works only in powershell console and not in cygwin, it cygwin it says that 但它只能在Powershell控制台中工作,而不能在cygwin中工作,cygwin表示
-bash: syntax error near unexpected token `&'
So it seems like that &
is treaten as bash character. 因此,似乎
&
被视为bash字符。 I tried to escape it in thousand ways, eg 我试图以千种方式逃脱它,例如
bash -c "'powershell { \$web = \"google.com\" ; & ping.exe \$web }'"
But this is the output 但这是输出
bash: powershell { $web = "google.com" ; & ping.exe $web }: command not found
Thank you for a hint. 谢谢你的提示。
UPDATE 更新
I am able to do this: 我能够做到这一点:
powershell "Invoke-Command -ScriptBlock {\$env:FOO = \"google.com\" ; & ""C:\my\script.ps1"" }"
But when I am trying to access that FOO
variable throught $env:FOO
it is empty, it seems like I am unable to do so because that script is running in another scope or what ... 但是,当我尝试通过
$env:FOO
访问该FOO
变量时,它是空的,似乎无法执行此操作,因为该脚本在另一个作用域或其他范围内运行...
this command will pass an environment variable from cygwin ( $FOO="bar"
) to powershell, then run another powershell command ( dir env:
) which lists the environment variables (proving that it was set): 此命令会将一个环境变量从cygwin(
$FOO="bar"
)传递给powershell,然后运行另一个powershell命令( dir env:
:),其中列出了环境变量(证明已设置):
powershell "\$env:FOO=\"bar\";dir env:"
replace the dir env:
part with your script call and this should work for you. 用您的脚本调用替换
dir env:
part,这应该对您有用。
edit: here's the command (quoted slightly differently) including the call to an external script: 编辑:这是命令(引号略有不同),包括对外部脚本的调用:
powershell '$env:FOO="bar";& "C:\script name.ps1"'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.