在Powershell中解析.xml文件时,输出没有@ {var = ...}的哈希表

时间:2015-04-01 14:33:49

标签: xml powershell

我是Powershell的新手,并尝试使用以下代码解析.xml文件:

[xml] $Alloc_macro = get-content ".\Allocation_macro.xml"
$newlist = @()
foreach ($layer1 in $Alloc_macro.dmob.module.macro.block[4].block)
{
    $condition = ($layer1 | select condition)
    foreach ($layer2 in $layer1.for)
    {
        $var = ($layer2 | select var)
        $in = ($layer2 | select in)
        $newlist += New-Object -TypeName psobject -Property @{ condition = $condition; var = $var; in = $in }
    }
}
$newlist

结果如下:

condition                                             var                                                  in
---------                                             ---                                                  --
@{condition=@[AllocationFlag]}                        @{var=AllocationNumber}                              @{in=*[EQ](##PersonalNonPersonal##,"Personal",@[A...
@{condition=*[NOT](@[AllocationFlag])}

我的问题是,我可以得到这样的东西:

condition                                             var                                                  in
---------                                             ---                                                  --
@[AllocationFlag]                                     AllocationNumber                              *[EQ](##PersonalNonPersonal##,"Personal",@[A...
*[NOT](@[AllocationFlag])

谢谢!

1 个答案:

答案 0 :(得分:1)

更改此

$condition = ($layer1 | select condition)

到这个

$condition = ($layer1 | select -expand condition)