Powershell - StreamWriter只写第一行

时间:2016-04-19 08:16:00

标签: powershell export-to-csv streamwriter

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

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

我是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()

写输出以正确的方式显示输出

谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个:

$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"