2016-11-15 57 views
0

所以我有这个脚本,从XML的数据,特别是机器的名称,然后每个ID号的列表。我可以将每个变量都加入变量,但是当我试图列出所有的id时,只显示每个变量的第一个元素。我认为Export-CSV -Append可以解决这个问题,但迄今为止一无所获。导出CSV只返回第一个元素

$path="C:\Users\Desktop\DataIDs" 
$xmls = Get-ChildItem $path\* -Recurse | where {$_.Name -like '*DataIDX*'} 

$results = New-Object psobject 

ForEach ($xml in $xmls) { 
    [xml]$results = Get-Content $xml 
    $Name = $results.Benchmark.TestResult.target 
    $VID1 = @($results.Benchmark.TestResult.'rule-result' | where {$_.result -eq 'fail'}).Version | Sort-Object 
    $VID = $VID1 | Out-String 

    ForEach ($id in $VID) { 
     $results | Add-Member -MemberType NoteProperty -Name $Name -Value $id 
     } 
} 

$results | Export-Csv $path\results.csv -NoTypeInformation -Append 
Remove-Variable results 

回答

0

嗯,你追加成员的方式给出了这一切:

Add-Member -MemberType NoteProperty -Name $Name -Value $id 

你总是追加相同的成员。所以其他的追加内容是抛出“该成员已经在那里,不能写入该属性”或类似的东西。

ps。我的不好,我没有正确阅读原始问题。我编辑了答案来正确解释它。

0

您需要使用一个数组来存储所有$results对象,你每次都重写它...

编辑:还有另一个问题,我看到的是,你的$VID是一个String对象,你不能遍历字符串对象,你会得到字符...我相信这是不是你所需要的,去掉Out-String,然后再试一次

试试这个:

$path="C:\Users\Desktop\DataIDs" 
$xmls = Get-ChildItem $path\* -Recurse | where {$_.Name -like '*DataIDX*'} 

$resultsArray = @() 
ForEach ($xml in $xmls) { 
    [xml]$results = Get-Content $xml 
    $Name = $results.Benchmark.TestResult.target 
    $VID1 = @($results.Benchmark.TestResult.'rule-result' | where {$_.result -eq 'fail'}).Version | Sort-Object 
    $VID = $VID1 

    ForEach ($id in $VID) { 
     $results | Add-Member -MemberType NoteProperty -Name $Name -Value $id 
     } 
$resultsArray += $results 
} 

$resultsArray | Export-Csv $path\results.csv -NoTypeInformation -Append 
Remove-Variable results 
+0

感谢对于q uick回复,尝试了这一点,即时获取“你不能调用一个空值表达式的方法”,但你的话听起来正确 – dblfaultventura

+0

编辑我的答案 – Avshalom