0

I try convert an csv file to html with Powershell. It only takes the last line in the csv file and not all lines.

I am new to powershell, so hope someone can help me


$users = import-csv “C:\fso\names.csv”
$Report = ForEach ($item in $users)
{
    # I like to assign each property value to a simpler named variable, but it’s not necessary
    $firstName = $item.(“fornavn”)
    $lastName = $item.(“efternavn”)
}
$Report | ConvertTo-Html -Head $css -Body "<h1>Adresseliste</h1>`n<h5>Generated on $(Get-Date)</h5><table><tr><td><p>$firstname<p><td><p>$lastname</p></td></td></tr></table>" | Out-File "C:\fso\test.html"
  • Usually this question would be better suited for StackOverflow as it's not really within the scope of superuser. Usually you will find some information about what's on topic for an Exchange within the help section. If you haven't done already maybe take your time to read some of it like the how to ask section. It will help you to get better and probably faster answers. – Seth Sep 19 '18 at 13:09
0

You never really assign anything to $Report. You would need to do the HTML conversion within the loop. Currently you loop through each item and overwrite $firstName and $lastName with each iteration. You finish the loop and have the last value in it to use for your final output.

To provide an example:

Lets assume you want to sum the number one through ten using a loop. What you're currently doing is what follows.

$numbers = 1..10
$sum = 0
foreach($number in $numbers){
    $sum = $number
}
Write-Output $sum

You're assigning whatever number you're currently holding to be the sum. What you actually want to do in this case would be to add to the sum with each iteration.

$numbers = 1..10
$sum = 0
foreach($number in $numbers){
    $sum = $sum + $number
}
Write-Output $sum

What does that mean for your code? You need to generate your HTML within the loop. Your approach of assinging the output of the foreach loop to the $repot variable can actually be used for this. It's just currently not generating any output because you're just doing an assignment (that hopefully doesn't error). The following modification would give you a string with the output of each first and last name for each item. You could use this to generate your HTML. An alternative approach would be similar by using $report as a sum an adding to it each iteration.

$Report = ForEach ($item in $users)
{
    $firstName = $item.(“fornavn”)
    $lastName = $item.(“efternavn”)

    Write-Output $firstname,$lastName
}

P.S. be aware that foreach and ForEach-Object are different things. You will probably encounter the latter more often especially if you use the shorthand %. One major difference is that you can't use continue or break within the latter but instead would need to use another mechanism like return.

0

Sorry for my stupid questions

But i can now print all the lines through write-output, but i can't get the html output to work, can you maybe help me in the right direction?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.