2012-12-11 61 views
0

我已經安裝了cygwin,我即將執行一個用powershell編寫的腳本。我不喜歡這樣寫道:如何傳播Windows環境變量的PowerShell運行從cygwin

powershell "& ""C:\my\script.ps1""" 

可正常工作,我必須這樣做,那是因爲在劇本我執行另一個外部命令等等...

我想補充一些環境變量,該腳本,所以我想寫點東西像

powershell "$FOO="bar"; & ""C:\my\script.ps1""" 

這樣我就可以訪問$FOO變量在腳本和用它做什麼。這個想法是,如果該變量沒有定義,我使用一些默認值。我知道這也可以通過一些環境變量來實現,或者我可以將這些變量放到配置文件(profile.ps1)中,但我想擺脫那個文件,所以我不需要任何東西,我可以用變量覆蓋默認值我展示出。

而是說:

=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 

所以我在想,這樣的事情可能工作:

powershell { $web = "google.com" ; & ping.exe $web } 

但它只能在PowerShell控制檯,而不是在Cygwin的,它在cygwin它說那

-bash: syntax error near unexpected token `&' 

所以它看起來像&被處理爲bash c haracter。我試圖用千種方法逃避它,例如

bash -c "'powershell { \$web = \"google.com\" ; & ping.exe \$web }'" 

但這是輸出

bash: powershell { $web = "google.com" ; & ping.exe $web }: command not found 

謝謝你的提示。

UPDATE

我能夠做到這一點:

powershell "Invoke-Command -ScriptBlock {\$env:FOO = \"google.com\" ; & ""C:\my\script.ps1"" }" 

但是,當我試圖訪問FOO變量throught $env:FOO它是空的,好像我不能這樣做因爲該腳本運行在另一個範圍或什麼...

回答

0

此命令將傳遞一個環境變量從cygwin($FOO="bar")到powershell,然後ru ñ另一個PowerShell命令(dir env:)列出的環境變量(證明它被設置):

powershell "\$env:FOO=\"bar\";dir env:" 

與腳本調用替換dir env:一部分,這應該爲你工作。

編輯:這裏的命令(報價略有不同),包括調用外部腳本:

powershell '$env:FOO="bar";& "C:\script name.ps1"' 
相關問題