1

在查询 Power Shell 对象的属性时,我想知道列出的属性是否具有可能为我提供有用信息的子属性,但 Get-Member 命令和 Select-Object -ExpandProperty 参数都没有为我提供获取方法前面所有属性的信息。

例如,如果我对 X509 证书对象的实例执行“Get-Member -MemberType Property”命令,我会得到一个包含 18 个属性的列表,包括“Archived”、“Extensions”、“FriendlyName”等。

这些属性中的大多数没有子属性,但至少有一个——“扩展”属性——有。

反过来,其中一些子属性具有自己的子属性。

我需要在一个查询中预先获取所有这些信息,而不是对每一个都进行试验,看看我是否发现了一些有趣的东西。

有没有办法获取这些信息,或者有人编写了一个查询来显示顶级属性的所有子属性?

我环顾四周并没有发现任何东西。

我尝试编写一个查询脚本,但到目前为止它还没有产生好的结果。

谢谢你。

4

3 回答 3

1

我通常通过转换为 json 来完成您所寻求的。

默认情况下,ConvertTo-Json深度为 4 个元素。由于您只想要顶级属性及其子属性,因此您可以-depth2.

#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

以下是结果查询的部分内容:

{
    "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"
                       },

(我只贴了一小段)

您可以轻松查看扩展等属性、它们的子属性和关联的值。

附加说明

如果省略-depth参数 for ConvertTo-Json,它将递归到 4 的深度。

深度可配置多达 100 个级别。话虽如此,某些对象将具有在对象上递归的属性,因此除非需要,否则您不应不必要地将最大值放在那里。

于 2020-01-14T03:03:54.897 回答
1

这样的事情对我来说是“核选项”。通常我还是找不到我想要的东西:

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

另请参阅如何列出 PowerShell 对象的所有属性

于 2020-01-14T15:18:52.443 回答
0

我通常在要扩展的属性上使用Format-Customwith a 。-Depth这是仅显示 Extensions 属性的示例。

将默认$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

输出:

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
    ]

}
于 2020-01-14T05:52:46.687 回答