【问题标题】:Whats the difference between $ExecutionContext.SessionState.Path.CurrentLocation and $pwd in Powershell?Powershell 中的 $ExecutionContext.SessionState.Path.CurrentLocation 和 $pwd 有什么区别?
【发布时间】:2017-06-01 01:44:55
【问题描述】:

查看Powershell的提示功能,发现$ExecutionContext.SessionState.Path.CurrentLocation是用来获取当前路径的,这和$pwd有什么区别?

【问题讨论】:

    标签: shell powershell prompt


    【解决方案1】:
    PS C:\> $ExecutionContext.SessionState.Path.CurrentLocation
    
    小路
    ----
    C:\
    
    PS C:\> $ExecutionContext.SessionState.Path.CurrentLocation.GetType().FullName
    System.Management.Automation.PathInfo
    PS C:\> $PWD
    
    小路
    ----
    C:\
    
    PS C:\> $PWD.GetType().FullName
    System.Management.Automation.PathInfo

    所以,基本上区别在于$ExecutionContext.SessionState.Path.CurrentLocation$PWD 需要更多的输入。

    【讨论】:

      【解决方案2】:

      $pwd 确实是从$ExecutionContext 获取值。

      这两个变量之间的主要区别是$pwd 可以被覆盖,但$ExecutionContext 是常量(只读)。

      $ExecutionContext 旨在模仿 cmdlet 作者可用的界面。 $pwd 只是获取当前路径的便捷方式。

      所以,如果您需要获取路径,建议使用$ExecutionContext,而不用担心有人会弄乱$pwd 的值。

      【讨论】:

        【解决方案3】:

        $ExecutionContext.SessionState.Path.CurrentLocation$PWD其实是有区别的。

        当你使用$PWD实际上你会得到$ExecutionContext.SessionState.Path.CurrentLocation的结果,

        而如果您使用$ExecutionContext,您将获得更多关于执行上下文的属性。

        【讨论】:

        • 这真的是答案吗?当然,ExecutionModel 本身会比SessionState.Path.CurrentLocation 拥有更多的信息。
        • @Christian.K:我的意思是它就像一棵包含许多内容分支的树,其中一个是 $PWD 。但不是主要分支,它位于像 sessionstate.path 之类的次要分支中
        • 是的,但是 OP 询问“branches”之间的区别:$ExecutionContext.SessionState.Path.CurrentLocation 和 $PWD。​​span>
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-28
        • 1970-01-01
        • 1970-01-01
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 2016-10-04
        相关资源
        最近更新 更多