windows PowerShell:仅为单个命令设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1420719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerShell: Setting an environment variable for a single command only
提问by miracle2k
On Linux, I can do:
在 Linux 上,我可以这样做:
$ FOO=BAR ./myscript
to call "myscript" with the environment variable FOO being set.
在设置环境变量 FOO 的情况下调用“myscript”。
Is something similar possible in PowerShell, i.e. without having to first set the variable, call the command, and then unset the variable again?
在 PowerShell 中是否有类似的可能,即不必先设置变量,调用命令,然后再次取消设置变量?
To be more clear about my use case - I don't want to use this as part of a script. Rather, I have a third-party script whose behavior I can control using environment variables, but, in this case, not command line arguments. So being able to alternate between typing
为了更清楚地了解我的用例 - 我不想将其用作脚本的一部分。相反,我有一个第三方脚本,我可以使用环境变量控制其行为,但在这种情况下,不是命令行参数。所以能够在打字之间交替
$ OPTION=1 ./myscript
and
和
$ ./myscript
would just be very handy.
会非常方便。
回答by Keith Hill
Generally, it would be better to pass info to the script via a parameter rather than a global (environment) variable. But if that is what you need to do you can do it this way:
通常,通过参数而不是全局(环境)变量将信息传递给脚本会更好。但如果这是你需要做的,你可以这样做:
$env:FOO = 'BAR'; ./myscript
The environment variable $env:FOO can be deleted later like so:
环境变量 $env:FOO 可以稍后删除,如下所示:
Remove-Item Env:\FOO
回答by kizzx2
I got motivated enough about this problem that I went ahead and wrote a script for it: with-env.ps1
我对这个问题有足够的动力,我继续为它编写了一个脚本:with-env.ps1
Usage:
用法:
with-env.ps1 FOO=foo BAR=bar your command here
# Supports dot-env files as well
with-env.ps1 .\.env OTHER_ENV=env command here
On the other hand, if you install Gowyou can use env.exe
which might be a little more robust than the quick script I wrote above.
另一方面,如果你安装Gow,你可以使用env.exe
它可能比我上面写的快速脚本更健壮一些。
Usage:
用法:
env.exe FOO=foo BAR=bar your command here
# To use it with dot-env files
env.exe $(cat .env | grep.exe -v '^#') SOME_OTHER_ENV=val your command
回答by Jason R. Coombs
To accomplish the equivalent of the Unix syntax, you not only have to set the environment variable, but you have to reset it to its former value after executing the command. I've accomplished this for common commands I use by adding functions similar to the following to my PowerShell profile.
要完成 Unix 语法的等效项,您不仅必须设置环境变量,而且必须在执行命令后将其重置为以前的值。我已经通过向我的 PowerShell 配置文件添加类似于以下内容的函数来为我使用的常用命令完成此操作。
function cmd_special()
{
$orig_master = $env:app_master
$env:app_master = 'http://host.example.com'
mycmd $args
$env:app_master = $orig_master
}
So mycmd
is some executable that operates differently depending on the value of the environment variable app_master
. By defining cmd_special
, I can now execute cmd_special
from the command line (including other parameters) with the app_master
environment variable set... and it gets reset (or even unset) after execution of the command.
mycmd
某些根据环境变量的值进行不同操作的可执行文件也是如此app_master
。通过定义cmd_special
,我现在可以cmd_special
在app_master
设置了环境变量的情况下从命令行(包括其他参数)执行...并且在执行命令后它会被重置(甚至取消设置)。
Presumably, you could also do this ad-hoc for a single invocation.
据推测,您也可以为单个调用临时执行此操作。
& { $orig_master = $env:appmaster; $env:app_master = 'http://host.example.com'; mycmd $args; $env:app_master = $orig_master }
It really should be easier than this, but apparently this isn't a use-case that's readily supported by PowerShell. Maybe a future version (or third-party function) will facilitate this use-case. It would be nice if PowerShell had a cmdlet that would do this, e.g.:
它确实应该比这更容易,但显然这不是 PowerShell 容易支持的用例。也许未来的版本(或第三方功能)会促进这个用例。如果 PowerShell 有一个可以执行此操作的 cmdlet,那就太好了,例如:
with-env app_master='http://host.example.com' mycmd
Perhaps a PowerShell guru can suggest how one might write such a cmdlet.
也许 PowerShell 专家可以建议如何编写这样的 cmdlet。
回答by NReilingh
Considering that CMD is the native CLI on the Windows kernel (and is still the automation interface for lots of tools), you may be executing your PowerShell script with powershell.exe
from the CMD prompt or an interface that accepts CMD console statements.
考虑到 CMD 是 Windows 内核上的本机 CLI(并且仍然是许多工具的自动化接口),您可能正在使用powershell.exe
CMD 提示符或接受 CMD 控制台语句的接口执行 PowerShell 脚本。
If you are using the -File
parameter to pass your script to powershell.exe
, no other PowerShell code can be used to set an environment variable for the script to access, so instead you can set your environment variables in the CMD environment before calling powershell.exe
:
如果您使用-File
参数将脚本传递给powershell.exe
,则不能使用其他 PowerShell 代码来设置脚本访问的环境变量,因此您可以在调用之前在 CMD 环境中设置环境变量powershell.exe
:
> set foo=bar && powershell.exe -File .\script.ps1
A single &
will also work, but will allow the command to continue if the set
failed for some reason. (Is this even possible? I have no idea.)
单个&
也可以工作,但如果由于set
某种原因失败,将允许命令继续。(这可能吗?我不知道。)
Also, it may be safer to wrap "foo=bar"
in quotes so that nothing following gets passed to set
as the variable contents.
此外,"foo=bar"
用引号括起来可能更安全,这样后面的任何内容都不会set
作为变量内容传递给。
回答by i255d
https://gist.github.com/bobalob/6632690e33747c13a8c00875ee686be2
https://gist.github.com/bobalob/6632690e33747c13a8c00875ee686be2
$ENV:ARM_SUBSCRIPTION_ID = ""
$ENV:ARM_CLIENT_ID = ""
$ENV:ARM_CLIENT_SECRET = "" # This should end with an '=' symbol
$ENV:ARM_TENANT_ID = ""
回答by Andy Schneider
You can scope variables to functions and scripts.
您可以将变量范围限定为函数和脚本。
$script:foo = "foo"
$foo
$function:functionVariable = "v"
$functionVariable
New-Variable also has a -scope parameter if you want to be formal and declare your variable using new-variable.
如果您想要正式并使用 new-variable 声明您的变量,则 New-Variable 也有一个 -scope 参数。