2016-08-24 42 views
0

美好的一天。 我導入的CSV文件到一個變量如何替換powershell數組的每個元素屬性中的符號?

$header = "property1","property2" 
$dt = Import-Csv $tempfile -delimiter "," -header $header 

,現在想更換每個單元中的一個字母。我知道如何明確指定每個屬性

$dt | % { 
$_.'property1' = $_.'property1'.replace("'","") 
$_.'property2' = $_.'property2'.replace("'","") 
} 

,但不能明確如何做到這一點,而不明確指定數組元素的屬性。 此示例僅使用兩個屬性,但在實際腳本中大約有30個。

回答

1

您可以創建一個循環來運行對象上的屬性。這允許您訪問修改標題下的值。

Import-Csv $tempfile -Header $header | ForEach-Object { 
    foreach ($property in $_.PSObject.Properties) { 
     $property.Value = $property.Value.Replace("'", "") 
    } 
    # Output the modified line 
    $_ 
}