Powershell GetEnvironmentVariable与$ Env [英] Powershell GetEnvironmentVariable vs $Env

查看:141
本文介绍了Powershell GetEnvironmentVariable与$ Env的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过几次尝试通过命令行使用命令但无法识别该命令的情况.我将其范围缩小到与环境变量有关的问题.在每种情况下,当我使用基础C#方法而不是简写$ env:myVariable

I have run into a couple cases where I am trying to use a command via command line, but the command is not recognized. I have narrowed it down to an issue with environment variables. In each case, the variable is present when I retrieve the variable with the underlying C# method, but not with the shorthand, $env:myVariable

例如,如果我这样检索变量,我将获得一个值.

For example, if I retrieve the variable like this, I will get a value.

 [Environment]::GetEnvironmentVariable('ChocolateyInstall', 'Machine')

但是,如果我这样检索变量,则不会返回任何内容

But, if I retrieve the variable like this, nothing is returned

$env:ChocolateyInstall

然后我必须做这样的事情才能使我的命令生效.

I then have to do something like this to to get my command to work.

$env:ChocolateyInstall = [Environment]::GetEnvironmentVariable('ChocolateyInstall', 'Machine')

关于为什么必须这样做,我一直找不到很好的解释.我看过该文档,但对我而言,什么都没有.理想情况下,我想安装CLI,然后不必处理检查和分配环境变量以使命令正常工作.

I have not been able to find a good explanation as to why I have to do this. I've looked at this documentation, but nothing stands out to me. Ideally, I would like to install a CLI and then not have to deal with checking for and assigning environment variables for the command to work.

推荐答案

打开PowerShell会话时,所有永久存储的环境变量 1 将被加载到环境驱动器中( Env:)当前会话(

When opening a PowerShell session, all permanently stored environment variables1 will be loaded into the Environment drive (Env:) of this current session (source):

环境驱动器是包含环境的平面命名空间特定于当前用户会话的变量.

The Environment drive is a flat namespace containing the environment variables specific to the current user's session.

文档您已链接状态:

在PowerShell中更改环境变量时,更改仅影响当前会话.此行为类似于行为Windows Command Shell中的 Set 命令和 Setenv 命令在基于UNIX的环境中.更改机器或用户中的值作用域,则必须使用 System.Environment 类的方法.

When you change environment variables in PowerShell, the change affects only the current session. This behavior resembles the behavior of the Set command in the Windows Command Shell and the Setenv command in UNIX-based environments. To change values in the Machine or User scopes, you must use the methods of the System.Environment class.

因此,像这样定义/更改环境变量:

So defining/changing an environment variable like this:

$env:ChocolateyInstall = [Environment]::GetEnvironmentVariable('ChocolateyInstall', 'Machine')

将对当前会话进行更改,从而立即生效,但也仅对当前会话有效.

Will change it for the current session, thus being immediately effective, but will also only be valid for the current session.

[System.Environment] 的方法更加精细.您可以在此处选择要解决的环境变量范围.共有三个范围:

The methods of [System.Environment] are more fine grained. There you can choose which environment variable scope to address. There are three scopes available:

  • 机器
  • 用户
  • 过程

Process 范围等效于环境驱动器,并涵盖当前会话中可用的环境变量. Machine User 作用域解决了永久存储的环境变量 1 .您可以从特定范围获取变量,如下所示:

The Process scope is equivalent to the Environment drive and covers the environment variables available in your current session. The Machine and the User scope address the permanently stored environment variables1. You can get variables from a particular scope like this:

[Environment]::GetEnvironmentVariable('ChocolateyInstall', 'Machine')

并设置为:

[Environment]::SetEnvironmentVariable('ChocolateyInstall', 'any/path/to/somewhere', 'Machine')

如果要在当前PowerShell会话中使用 Machine User 范围中的新变量,则必须创建一个新变量.但是请不要从当前的PowerShell会话中打开新的PowerShell会话,因为它将随后继承当前PowerShell会话中的所有环境变量(

If you want to have new variables from the Machine or User scope available in your current PowerShell session, you have to create a new one. But don't open a new PowerShell session from your current PowerShell session, as it will then inherit all environment variables from your current PowerShell session (source):

环境变量与PowerShell中的其他类型的变量不同,由子进程继承,例如本地后台作业和模块成员在其中运行的会话.这使得环境变量非常适合存储父级和父级中所需的值和子进程.

Environment variables, unlike other types of variables in PowerShell, are inherited by child processes, such as local background jobs and the sessions in which module members run. This makes environment variables well suited to storing values that are needed in both parent and child processes.

因此,为了解决您所描述的问题,您很可能在已经打开PowerShell会话的同时更改了永久存储的环境变量 1 .如果是这样,您只需要打开一个新的(确实是新的,见上文)会话,就可以通过环境驱动器访问环境变量.需要明确的是,打开一个新会话甚至会重新加载 Machine 范围的环境变量.无需重新启动.

So, to address the problem you described, you most probably changed your permanently stored environment variables1, while already having an open PowerShell session. If so, you just need to open a new (really new, see above) session and you will be able to access your environment variables via the Environment drive. Just to be clear, opening a new session will even reload environment variables of the Machine scope. There is no reboot required.

1 是您在进入系统控制面板,选择高级系统设置并在高级标签,点击环境变量.这些变量涵盖了 User Machine 范围.或者,您可以通过执行以下命令直接打开此GUI:

1 That are the environment variables you see in the GUI when going to the System Control Panel, selecting Advanced System Settings and on the Advanced tab, clicking on Environment Variable. Those variables cover the User and the Machine scope. Alternatively, you can open this GUI directly by executing:

rundll32 sysdm.cpl,EditEnvironmentVariables

这篇关于Powershell GetEnvironmentVariable与$ Env的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆