2017-03-02 83 views
0

我想從以下兩個數組搜索並導出到csv的行/結果,但我看不到如何處理輸出到文件。powershell - 從foreach數組結果導出csv

$inputfile = import-csv 'C:\Users\Desktop\working\8a.csv' 

$OutAll1 = @() 

foreach($row in $inputfile) 
{ 

if($row.Parameter -contains "Type of file server") 
{ 

echo "Hooray for Type of File Server" 
$OutList = New-Object System.Object 
$OutList | Add-Member -type NoteProperty -Name "Parameter" -Value $line."Type of file server" 
$OutAll1 += $OutList 

} 

if($row.Parameter -contains "File server") 
{ 

echo "File Server" 
$OutList = New-Object System.Object 
$OutList | Add-Member -type NoteProperty -Name "Parameter" -Value $line."Type of file server"   
$OutAll1 += $OutList 
} 

這裏是被我輪不到輸出爲我的數組:

Export-Csv 'c:\users\desktop\working\out.csv' 

或者,如果我做了數組變量:

$data = $outall1 = @() 
foreach($row in $data) {export-csv 'c:\users\desktop\working\out.csv'} 

我的目標是把從結果第一個和第二個搜索詞並放入csv中,保留原始列名稱(即:「Component,Server,Parameter,Value」)

回答

0

您應該先複製行對象,例如$OutList = $row.PSObject.Copy()(這是適用於CSV處理的淺拷貝)。

但是,它更容易使用流水線和修改行變量$_直接:如果不需要進一步的處理

$csv = Import-Csv 'C:\Users\Desktop\working\8a.csv' | ForEach { 
    if ($_.Parameter -match 'Type of file server') { 
     Write-Information 'Hooray for Type of File Server' 
     $_.Parameter = 'foo' 
    } elseif ($_.Parameter -match 'File server') { 
     $_.Parameter = 'bar' 
    } 
    # now output the modified row so that ForEach collects it in $csv 
    $_ 
} 

,您可以直接導出到另一個CSV沒有$csv變量:

Import-Csv 'C:\Users\Desktop\working\8a.csv' | ForEach { 
    ...................... 
    $_ 
} | Export-Csv 'c:\users\desktop\working\out.csv' -NoTypeInformation -Encoding UTF8 

備註:

  • -contains運算符用於數組;字符串使用-match(也正則表達式)或-like(使用通配符)

  • 流水線收集所有輸出,包括echo,所以對於輔助消息使用除了Write-Output
    Write-InformationWrite-Host和其它小命令Write-

  • +=在數組的情況下重新創建整個數組:它將舊內容與新元素一起復制到新數組中。這可以說是在PowerShell中填充大型數組的最糟糕的方法。只有在循環內不是一次性操作時才使用+=,或者該數組很小並且時間無關緊要。

0

我已經解決了這個問題。我很欣賞讓我發佈的網站。以下是解決它的幾個主要部分。 1.我需要添加額外的「添加成員noteproperty」來獲得我想要的部分行。 2.我正確地嘗試「$ varible | export-csv」;然而,在我發佈之前我沒有成功,也沒有明白爲什麼。這是最後的工作代碼:

$inputfile = import-csv 'C:\Users\Desktop\working\8a.csv' 


$OutAll1 = @() 


foreach($row in $inputfile){ 

if($row.Parameter -like "Type of file server"){ 

    echo "Hooray for Type of File Server" 
    $OutList = New-Object System.Object 
    $OutList | Add-Member -type NoteProperty -Name "Component" -Value $row.Component 
    $OutList | Add-Member -type NoteProperty -Name "Server" -Value $row.Server 
    $OutList | Add-Member -type NoteProperty -Name "Parameter" -Value $row.Parameter 
    $OutAll1 += $OutList 

} 

if($row.Parameter -contains "File server"){ 

    echo "File Server" 
    $OutList = New-Object System.Object 
    $OutList | Add-Member -type NoteProperty -Name "Component" -Value $row.Component 
    $OutList | Add-Member -type NoteProperty -Name "Server" -Value $row.Server 
    $OutList | Add-Member -type NoteProperty -Name "Parameter" -Value $row.Parameter  
    $OutAll1 += $OutList 
    } 

$Outall1 | Export-csv 'c:\out.csv' 

我總是錯誤地在數組中使用$ row和$ line。