简体   繁体   English

如何在 Power Shell 中查看所有子属性

[英]How To View All Child Properties in Power Shell

When querying for the properties of a Power Shell object, I want to know if the properties listed have child properties that might provide me useful info, but neither the Get-Member command nor the Select-Object -ExpandProperty parameter offer me a way to get that information for all the properties up front.在查询 Power Shell 对象的属性时,我想知道列出的属性是否具有可能为我提供有用信息的子属性,但是 Get-Member 命令和 Select-Object -ExpandProperty 参数都没有为我提供获取前面所有属性的信息。

For instance, if I execute a "Get-Member -MemberType Property" command against an instance of an X509 certificate object, I get a list of 18 properties including "Archived", "Extensions", "FriendlyName", etc.例如,如果我对 X509 证书对象的实例执行“Get-Member -MemberType Property”命令,我会得到一个包含 18 个属性的列表,包括“Archived”、“Extensions”、“FriendlyName”等。

Most of those properties do not have child properties, but at least one - the "Extensions" property - does.大多数这些属性没有子属性,但至少有一个 - “扩展”属性 - 有。

In turn, some of those child properties have their own child properties.反过来,其中一些子属性有自己的子属性。

I need to get all that info up front on one query, not experiment with each one to see if I discover something interesting.我需要在一个查询中预先获取所有信息,而不是对每个查询都进行试验,看看我是否发现了一些有趣的东西。

Is there a way to get this info or has someone written a query that will display all the child properties of top-level properties?有没有办法获取此信息,或者有人编写了一个查询来显示顶级属性的所有子属性?

I've looked around quite a bit and not found anything.我环顾四周,没有发现任何东西。

I tried scripting out a query, but thus far it has not produced good results.我尝试编写查询脚本,但到目前为止还没有产生好的结果。

Thank you.谢谢你。

I usually accomplish what you seek simply by converting to json.我通常通过转换为 json 来完成你所寻求的。

By default, ConvertTo-Json have a depth of 4 elements.默认情况下, ConvertTo-Json的深度为 4 个元素。 Since you only want the top properties and their child, you can reduce the -depth to 2 .由于您只想要顶级属性及其子级,您可以将-depth减少到2

#Selecting the first certificate just for demonstration purposes.
$YourObject = (get-childitem -Path 'Cert:\CurrentUser\CA\')[0]

# This will work with any objects.
$YourObject | ConvertTo-Json -Depth 2

Here's a partial look at the resulting query:下面是对结果查询的部分查看:

{
    "Archived":  false,
    "Extensions":  [
                       {
                           "Critical":  false,
                           "Oid":  "System.Security.Cryptography.Oid",
                           "RawData":  "48 33 48 31 6 8 43 6 1 5 5 7 48 1 134 19 104 116 116 112 58 47 47 115 50 46 115 121 109 99 98 46 99
111 109"
                       },
                       {
                           "CertificateAuthority":  true,
                           "HasPathLengthConstraint":  true,
                           "PathLengthConstraint":  0,
                           "Critical":  true,
                           "Oid":  "System.Security.Cryptography.Oid",
                           "RawData":  "48 6 1 1 255 2 1 0"
                       },

(I only pasted a small snippet) (我只贴了一小段)

You can easily see the properties such as Extensions, their child properties and the associated values.您可以轻松查看扩展、它们的子属性和相关值等属性。

Additional note附加说明

If you omit the -depth parameter for ConvertTo-Json , it will recurse to a depth of 4.如果省略ConvertTo-Json-depth参数,它将递归到 4 的深度。

The depth is configurable up to 100 levels.深度最多可配置 100 个级别。 That being said, some object will have properties that recurse upon the object, so unless needed, you should not unnecessarily put the maximum value there.话虽如此,某些对象将具有在对象上递归的属性,因此除非需要,否则不应不必要地将最大值放在那里。

Something like this is the "nuclear option" for me.这样的事情对我来说是“核选项”。 Usually I don't find what I want anyway:通常我找不到我想要的东西:

get-process cmd | fc * | findstr /i whatever

See also How to list all properties of a PowerShell object另请参阅如何列出 PowerShell 对象的所有属性

I typically use Format-Custom with a -Depth on the properties you want to expand.我通常在要扩展的属性上使用带有-Depth Format-Custom Here's an example of displaying only the Extensions property.下面是一个仅显示 Extensions 属性的示例。

Changing the default $FormatEnumerationLimit value to -1 will also allow you to display all enumerable property values:将默认的$FormatEnumerationLimit值更改为 -1 还可以显示所有可枚举的属性值:

# Remove limit from enumeration limit which is 4 by default
$FormatEnumerationLimit=-1

# Use Format-Custom with the depth required:
Get-ChildItem -Path 'Cert:\CurrentUser\CA\' | Select-Object -First 1 -Property Extensions | Format-Custom -Depth 1

Output:输出:

class X509Certificate2
{
  Extensions = 
    [
      System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension
      System.Security.Cryptography.X509Certificates.X509Extension
      System.Security.Cryptography.X509Certificates.X509KeyUsageExtension
      System.Security.Cryptography.X509Certificates.X509Extension
      System.Security.Cryptography.X509Certificates.X509Extension
      System.Security.Cryptography.X509Certificates.X509Extension
      System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension
      System.Security.Cryptography.X509Certificates.X509Extension
    ]

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM