Reputation: 355
I have a list of ciphers that I need to identify and remove, and the easiest way for a non-developer to maintain this list is via a declaration similar to this:
$bannedCiphers = @{
"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"
}
}
}
Although I'm having trouble creating the correct syntax for nested objects within Powershell.
What is the correct way to create an object with nested properties like the one above?
Upvotes: 5
Views: 10099
Reputation: 4253
you can also create nested lists in a class.
class cChild{
[string] $id
[string] $field1
}
class cParent{
[string] $id
[string] $field1
[cChild[]] $child_list
}
one or more child classes can be nested in the parent as the variable $child_list. Classes set boundaries for acceptable data parsed from json.
Upvotes: 0
Reputation: 142
I thought I would re-post this answer and clarify my code a bit more using a custom object example.
Original solution without pscustomobject:
$bannedCiphers = @{
"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"
)
}
"Another RC4"= @{
"IsPermitted" = $false
"AffectedCiphers" = @(
"Cipher1",
"Cipher2",
"Cipher3",
"Cipher4"
)
}
}
The output of this solution will yield $bannedCiphers output:
Name Value
---- -----
Another RC4 {IsPermitted, AffectedCiphers}
RC4 128/128 {IsPermitted, AffectedCiphers}
My solution creating custom objects:
$bannedCiphers2 = [pscustomobject]@{
"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"
)
}
"Another RC4"= @{
"IsPermitted" = $false
"AffectedCiphers" = @(
"Cipher1",
"Cipher2",
"Cipher3",
"Cipher4"
)
}
}
The output for my solution will yield $bannedCiphers2 output:
RC4 128/128 Another RC4
----------- -----------
{IsPermitted, AffectedCiphers} {IsPermitted, AffectedCiphers}
original:
$bannedCiphers | Select-Object *
IsReadOnly : False
IsFixedSize : False
IsSynchronized : False
Keys : {Another RC4, RC4 128/128}
Values : {System.Collections.Hashtable, System.Collections.Hashtable}
SyncRoot : System.Object
Count : 2
vs:
$bannedCiphers2 | Select-Object *
RC4 128/128 Another RC4
----------- -----------
{IsPermitted, AffectedCiphers} {IsPermitted, AffectedCiphers}
Upvotes: 1
Reputation: 8538
This should do the trick...
$bannedCiphers = @{
"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"
)
}
}
Furthermore, the $bannedCiphers
hashtable could easily be converted to JSON (and back)
A la...
$bannedCiphers | ConvertTo-Json
...which outputs:
{
"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"
]
}
}
If you had this data in JSON format to begin with, you could import that into PowerShell like:
$myJsonData = @"
{
"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"
]
}
}
"@
$myNestedCiphers = $myJsonData | ConvertFrom-Json
Upvotes: 3