2016-11-15 59 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