Powershell - StreamWriter 只写第一行

Powershell - StreamWriter only write first line

我正在处理应该导入 csv 的代码 - 更改内容,然后导出到 csv。

我尝试使用 Export-Csv 导出 - 但它只写入字符串的长度 我也尝试过使用 StreamWriter - 但它只写 $Resultat once

我是 PowerShell 新手:-)

$workfile = import-csv "X:\powershell\converter\test.csv" -Delimiter ';'

ForEach ($item in $workfile)
{
$Id = $item.("ID")
$Maaler = $item.("Maaler")
$Fra = $item.("Fra")
$Til = $item.("Til") 

#Dato gymnastik
$Tilto = $item.("Til")
$Tilto = $Tilto.substring(0,10)


$Til = $Til.substring($Til.length - 8, 8)
$Forbrug = $item.("Forbrug")
$Enhed = $item.("Enhed")
$aflaesningstype = $item.("Aflæsningstype?")
$T = ".000+02:00"

$Resultat = $Tilto + "T" + $Til + $T + ',"' + $Maaler + '","' + '","' + '","' + '","' + '","' + $Forbrug + '","' + $Enhed + '","' + '255"'

Write-output "$Resultat"
}
$Resultat | Export-Csv "X:\powershell\behandlet\Output $(get-date -f dd-MM-yyyy-hh-mm-ss).csv" -Delimiter ',' -NoType

#$fhStream = [System.IO.StreamWriter] "X:\powershell\behandlet\Output $(get-date -f dd-MM-yyyy-HH-mm-ss).csv" 
#$fhStream.WriteLine($Resultat)
#$fhStream.Close()

Write-output 以正确的方式显示输出

谁能看出我做错了什么?

试试这个:

$workfile = import-csv "X:\powershell\converter\test.csv" -Delimiter ';'
$result = New-Object System.Collections.ArrayList
ForEach ($item in $workfile)
{
$Id = $item.("ID")
$Maaler = $item.("Maaler")
$Fra = $item.("Fra")
$Til = $item.("Til") 

#Dato gymnastik
$Tilto = $item.("Til")
$Tilto = $Tilto.substring(0,10)


$Til = $Til.substring($Til.length - 8, 8)
$Forbrug = $item.("Forbrug")
$Enhed = $item.("Enhed")
$aflaesningstype = $item.("Aflæsningstype?")
$T = ".000+02:00"

$result += $Tilto + "T" + $Til + $T + ',"' + $Maaler + '","' + '","' + '","' + '","' + '","' + $Forbrug + '","' + $Enhed + '","' + '255"'
}
$result | Out-File "X:\powershell\behandlet\Output $(get-date -f dd-MM-yyyy-hh-mm-ss).csv"