【问题标题】:Why does Powershell's Format-List * not output all properties为什么Powershell的Format-List *不输出所有属性
【发布时间】:2017-11-15 19:45:06
【问题描述】:

当我执行... | Format-List 时,其中一个属性是Path
当我执行... | Format-List * 时,缺少Path 属性。
为什么?

[DBG]: PS C:\Directory>> $MyInvocation.MyCommand | Format-List

Name        : 
CommandType : Script
Definition  : $MyInvocation.MyCommand | Format-List
Path        : 

[DBG]: PS C:\Directory>> $MyInvocation.MyCommand | Format-List *

HelpUri            : 
ScriptBlock        : $MyInvocation.MyCommand | Format-List *
Definition         : $MyInvocation.MyCommand | Format-List *
OutputType         : {}
Name               : 
CommandType        : Script
Visibility         : Public
ModuleName         : 
Module             : 
RemotingCapability : PowerShell
Parameters         : 
ParameterSets      : {}

【问题讨论】:

    标签: powershell


    【解决方案1】:

    表扬gms0ulman's answer...

    PowerShell 根据 format.ps1xml 文件向控制台显示对象。来自Formatting File Overview

    命令(cmdlet、函数和脚本)返回的对象的显示格式是使用格式化文件(format.ps1xml 文件)定义的。其中一些文件由 Windows PowerShell 提供,用于定义由 Windows PowerShell 提供的命令返回的对象的显示格式...

    PowerShell 在收到指令时,会在未另行通知时以特定方式显示对象。在您的情况下,这来自文件 PowerShellCore.format.ps1xml。通过检查$MyInvocation.MyCommand 的类型,我们知道它是一个System.Management.Automation.ScriptInfo 对象。知道我们可以从上述文件中提取格式详细信息。

    <View>
        <Name>System.Management.Automation.ScriptInfo</Name>
        <ViewSelectedBy>
            <TypeName>System.Management.Automation.ScriptInfo</TypeName>
        </ViewSelectedBy>
    
    
        <ListControl>
            <ListEntries>
                <ListEntry>
                   <ListItems>
                        <ListItem>
                            <PropertyName>Name</PropertyName>
                        </ListItem>
                        <ListItem>
                            <PropertyName>CommandType</PropertyName>
                        </ListItem>
                        <ListItem>
                            <PropertyName>Definition</PropertyName>
                        </ListItem>
                        <ListItem>
                            <PropertyName>Path</PropertyName>
                        </ListItem>
                    </ListItems>
                </ListEntry>
            </ListEntries>
        </ListControl>
    </View>
    

    所以我们看到它会尝试显示名称、命令类型、定义和路径。如您所知,为所有属性指定 * 将显示对象的所有现有属性1

    Path is null of course because of where you ran it from.

    1 该语句的一个小例外是一些常见的对象属性隐藏到 *.在这种情况下使用-Force 显示所有内容。无论您是否知道他们的名字,他们都可以访问。

    【讨论】:

      【解决方案2】:

      在控制台中运行时,Path 属性实际上并不存在。 Format-List - 没有* - 正在使用模板来确定要显示的属性。 很遗憾,我找不到文档链接来进一步解释这一点,以及如何配置——如果我这样做了,我会编辑。
      See Matt's answer

      您可以通过运行$MyInvocation.MyCommand | Get-Member 并注意Path 不是属性之一来看到这一点。有道理,因为您是在控制台中运行命令,而不是从脚本中运行。

      在脚本中,$MyInvocation.MyCommand | Format-List$MyInvocation.MyCommand | Format-List * 都包含 Path


      $MyInvocation 仅为脚本、函数和脚本块填充。

      来自about_Automatic_Variables。看起来 PowerShell 将您的命令视为脚本,以便(部分)填充 $MyInvocation

      【讨论】:

        猜你喜欢
        • 2012-05-24
        • 1970-01-01
        • 2022-11-19
        • 1970-01-01
        • 2018-06-15
        • 1970-01-01
        • 2019-09-05
        • 2015-07-06
        • 2015-11-22
        相关资源
        最近更新 更多