从对象内的对象中选择

时间:2017-02-21 16:24:11

标签: powershell

我在Powershell中有一组信息,它由对象中的对象组成。它深陷4个物体。有没有办法从最底层的对象中选择一个属性,同时保持在顶层?这有什么意义吗?

$result
active                    : active
security                  : @{waf=; acls=}
sealloaction              : 
siteDualFactorSettings    : @{enabled=False; version=0}
login_protect             : @{enabled=False; url_patterns=System.Object[]}
performance_configuration : @{advanced_caching_rules=; acceleration_level=standard; cache300x=False; cache_headers=System.Object[]}

$result.security

waf                      acls                    
---                      ----                    
@{rules=System.Object[]} @{rules=System.Object[]}

$result.security.waf

rules                                                                                                                                                                                                                                                            
-----                                                                                                                                                                                                                                                            
{@{action=api.threats.action.block_request; action_text=Block; id=api.threats.sql_injection; name=SQL Injection}, @{action=api.threats.action.alert; action_text=Alert Only;}

4 个答案:

答案 0 :(得分:0)

$myArray | where { $_.x.y.z -eq 'something' }

在功能上等同于

foreach ($object in $myArray)
{
    if ($object.x.y.z -eq 'something')
    {
        $object  # Writes $object to the output stream
    }
}

答案 1 :(得分:0)

这个答案的大部分是设置代码,但是你可以。就像wOxxOm说的那样。

$obj4 = New-Object -TypeName PsObject
$obj4 | Add-Member -MemberType NoteProperty -Name Foo -Value "Foo4"

$obj3 = New-Object -TypeName PsObject
$obj3 | Add-Member -MemberType NoteProperty -Name L3 -Value $obj4
$obj3 | Add-Member -MemberType NoteProperty -Name Foo -Value "Foo3"

$obj2 = New-Object -TypeName PsObject
$obj2 | Add-Member -MemberType NoteProperty -Name L2 -Value $obj3
$obj2 | Add-Member -MemberType NoteProperty -Name Foo -Value "Foo2"

$obj1 = New-Object -TypeName PsObject
$obj1 | Add-Member -MemberType NoteProperty -Name L1 -Value $obj2
$obj1 | Add-Member -MemberType NoteProperty -Name Foo -Value "Foo1"

以下是访问对象图中属性的方法。

$obj1.L1.L2.L3.Foo

答案 2 :(得分:0)

生成混合的主/嵌套级别选择:

    主阵列上的
  1. foreach
  2. select嵌套的东西+计算属性来引用迭代的主数组元素
  3. $results | ForEach {
        $r = $_
        $r.security.waf.rules | select *, @{N='domain'; E={$r.domain}}
    }
    

    或者作为一个单行:

    $results | %{ $r = $_; $r.security.waf.rules | select *, @{N='domain'; E={$r.domain}} }
    

答案 3 :(得分:0)

更简单的方法应该是使用$result | select ${L='label';E={$_.security.waf}},然后深入到所需的位置。

例如:


$obj1_child = New-Object -TypeName psobject 
$obj1_child | add-member -Name obj1_child -MemberType NoteProperty -Value "obj1_child"
$obj1_child | add-member -Name value1 -MemberType NoteProperty -Value "child_value1"

$obj1 = New-Object -TypeName psobject 
$obj1 | add-member -Name obj1 -MemberType NoteProperty -Value "obj1"
$obj1 | add-member -Name child -MemberType NoteProperty -Value $obj1_child

$obj2 = New-Object -TypeName psobject 
$obj2 | add-member -Name value1 -MemberType NoteProperty -Value "value1"

$parentObj = New-Object -TypeName psobject
$parentObj | Add-Member -MemberType NoteProperty -name string -value "parentObject"
$parentObj | Add-Member -MemberType NoteProperty -name OBJ1 -value $obj1
$parentObj | Add-Member -MemberType NoteProperty -name OBJ2 -value $obj2


$parentObj | select string,obj2, @{L='value of obj1 child';E={$_.obj1.child.value1}}

我在其中创建了一个$parentObj,其中包含一个字符串和两个对象($ obj1,$ obj2),在$obj1中创建了另一个对象($obj1_child)。现在,我希望$parentObj中的字符串和$obj1_child中的最后一个字符串可以正常工作:

$parentObj | select string, @{L='value of obj1 child';E={$_.obj1.child.value1}}

输出:

>> $parentObj | select string, @{L='value of obj1 child';E={$_.obj1.child.value1}}


string       value of obj1 child
------       -------------------
parentObject child_value1

不确定是否是您想要的。但这是我登陆这里时一直在寻找的东西,因此可能会对其他人有所帮助。 参考: https://devblogs.microsoft.com/scripting/access-objects-inside-other-objects-in-powershell-pipeline/