如何获取 PSCustom 对象的长度?

Ran*_*Dom 1 powershell json

以下存储在powershell中

#Maintainer Note: The leftmost parameter must match the registry key name exactly e.g. 'DES 56'
#For more information please check https://support.microsoft.com/en-us/kb/245030
$bannedCiphersJSON = @"
   {
    "RC4 128/128":{
        "IsPermitted":false,
        "AffectedCiphers":[
                        "SSL_RSA_WITH_RC4_128_MD5",
                        "SSL_RSA_WITH_RC4_128_SHA",
                        "TLS_RSA_WITH_RC4_128_MD5",
                        "TLS_RSA_WITH_RC4_128_SHA"
        ]
    },

    "Triple DES 168":{
        "IsPermitted":false,
        "AffectedCiphers":[
                        "SSL_RSA_WITH_3DES_EDE_CBC_SHA",
                        "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA" ,
                        "TLS_RSA_WITH_3DES_EDE_CBC_SHA" ,
                        "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA"
        ]
    },

    "RC4 56/128":{
        "IsPermitted":false,
        "AffectedCiphers":[
                        "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA"
        ]
    },

    "DES 56":{
        "IsPermitted":false,
        "AffectedCiphers":[
                        "SSL_RSA_WITH_DES_CBC_SHA"
        ]
    },

    "RC4 40/128":{
        "IsPermitted":false,
        "AffectedCiphers":[
                        "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
                        "TLS_RSA_EXPORT_WITH_RC4_40_MD5"
        ]
    },

    "RC2 40/128":{
        "IsPermitted":false,
        "AffectedCiphers":[
                        "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5",
                        "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5"
        ]
    },

    "MD5":{
        "IsPermitted":false,
        "AffectedCiphers":[
                        "SSL_RSA_EXPORT_WITH_RC4_40_MD5",
                        "SSL_RSA_WITH_RC4_128_MD5",
                        "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5",
                        "TLS_RSA_EXPORT_WITH_RC4_40_MD5",
                        "TLS_RSA_WITH_RC4_128_MD5",
                        "TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5"
        ]
    },

    "SHA":{
        "IsPermitted":false,
        "AffectedCiphers":[
                        "SSL_RSA_WITH_RC4_128_SHA",
                        "SSL_RSA_WITH_DES_CBC_SHA",
                        "SSL_RSA_WITH_3DES_EDE_CBC_SHA",
                        "SSL_RSA_EXPORT1024_WITH_DES_CBC_SHA",
                        "SSL_RSA_EXPORT1024_WITH_RC4_56_SHA",
                        "TLS_RSA_WITH_RC4_128_SHA",
                        "TLS_RSA_WITH_DES_CBC_SHA",
                        "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
                        "TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA",
                        "TLS_RSA_EXPORT1024_WITH_RC4_56_SHA"
        ]
    }
}
"@

$bannedCiphers =$bannedCiphersJSON  | ConvertFrom-Json

function Get-TLSProtocol{ 

    For ($i=0; $i -lt $bannedCiphers.Count; $i++)
    {

      write-output  $i 
    }
}

Get-TLSProtocol
Run Code Online (Sandbox Code Playgroud)

当我Get-Member对对象运行 a 时,每个对象似乎都是一个“音符属性”。因此,我认为数组定义不正确。(我的目标是获取可用于检查注册表的对象列表。

PS C:\users\golden> $bannedCiphers | get-member


   TypeName: System.Management.Automation.PSCustomObject

Name           MemberType   Definition
----           ----------   ----------
Equals         Method       bool Equals(System.Object obj)
GetHashCode    Method       int GetHashCode()
GetType        Method       type GetType()
ToString       Method       string ToString()
DES 56         NoteProperty System.Management.Automation.PSCustomObject DES 56=@{IsPermitted=False; AffectedCiphers=...
MD5            NoteProperty System.Management.Automation.PSCustomObject MD5=@{IsPermitted=False; AffectedCiphers=Sys...
RC2 40/128     NoteProperty System.Management.Automation.PSCustomObject RC2 40/128=@{IsPermitted=False; AffectedCiph...
RC4 128/128    NoteProperty System.Management.Automation.PSCustomObject RC4 128/128=@{IsPermitted=False; AffectedCip...
RC4 40/128     NoteProperty System.Management.Automation.PSCustomObject RC4 40/128=@{IsPermitted=False; AffectedCiph...
RC4 56/128     NoteProperty System.Management.Automation.PSCustomObject RC4 56/128=@{IsPermitted=False; AffectedCiph...
SHA            NoteProperty System.Management.Automation.PSCustomObject SHA=@{IsPermitted=False; AffectedCiphers=Sys...
Triple DES 168 NoteProperty System.Management.Automation.PSCustomObject Triple DES 168=@{IsPermitted=False; Affected...
Run Code Online (Sandbox Code Playgroud)

在powershell中定义对象以便我可以迭代最顶层的正确方法是什么?

Lee*_*ley 5

你有一个 PSCustomObject,而不是一个数组。该对象在属性中隐藏了一些数组,tho。[咧嘴笑]这是获取列表和计数的一种方法......

$AffectedCiphers = foreach ($PropName in $Test.PSObject.Properties.Name)
    {
    $Test.$PropName.AffectedCiphers
    }

'There are {0} ciphers in the Banned Ciphers list.' -f $AffectedCiphers.Count
Run Code Online (Sandbox Code Playgroud)

输出:

禁用密码列表中有 30 种密码。

以上是做什么的:

  • 调用.PSObject所有 powershell 对象的隐藏属性
  • 获取父对象中的普通属性列表
  • 遍历列表,获取.AffectedCiphers属性值(在本例中为一组值)
  • 将它们保存到 $AffectedCiphers
  • 获取项目的数量 $AffectedCiphers

希望有帮助,