Array html and skip line

matteu31 487 Reputation points
2021-07-27T14:33:53.24+00:00

Hello,

I don't know if what I would like to do can be done.

I use this sample to illustrate

$days= "monday","tuesday","wednesday"  
$result = foreach ($a in 1..4)  
{  
    [pscustomobject]@{  
        Day = $a  
        ScheduleDay = $days -join "`r`n"  
    }  
}  
  
$result | ConvertTo-Html | Out-File "c:\test.html"  
c:\test.html  

I would like to have the result in the next picture.

118304-2021-07-27-16h32-01.png

But my result is this one :

118375-2021-07-27-16h33-13.png

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,628 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 120K Reputation points MVP
    2021-07-27T15:27:53.91+00:00

    Hi @matteu31 ,

    please try this:

    $days = "monday", "tuesday", "wednesday"  
    $result = foreach ($a in 1..4) {  
        [pscustomobject]@{  
            Day         = $a  
            ScheduleDay = $days -join "::"  
        }  
    }  
    $html = $result | ConvertTo-Html  
    $html.Replace('::', '<br>') | Out-File "c:\temp\test.html"  
    c:\temp\test.html  
    

    Result looks like this here:

    118364-image.png

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Michael Taylor 58,451 Reputation points
    2021-07-27T15:08:18.427+00:00

    rn has no meaning in HTML. Use <br/> instead.

    0 comments No comments

  2. matteu31 487 Reputation points
    2021-07-27T15:20:56.123+00:00

    If I do :

    $days= "monday","tuesday","wednesday"  
     $result = foreach ($a in 1..4)  
     {  
         [pscustomobject]@{  
             Day = $a  
             ScheduleDay = $days -join "<br/>"  
         }  
     }  
          
     $result | ConvertTo-Html | Out-File "c:\test.html"  
     c:\test.html  
    

    Same result...

    118318-2021-07-27-17h21-29.png

    I can see it's because <br/> is modified

    118377-2021-07-27-17h22-23.png


  3. matteu31 487 Reputation points
    2021-07-27T15:34:55.363+00:00

    Hello,

    Thanks for your answer. It works perfectly :)

    0 comments No comments

  4. Rich Matheisen 47,856 Reputation points
    2021-07-27T18:35:48.843+00:00

    If you want borders around your cells you'll have to use styles. Here's an example:

    # CSS table style
    $h = @"
    <style>
        table, th,td {
            border-width:     1px;
            border-style:     solid;
            border-color:     black;
            border-collapse:  collapse;
        }
        tr {
            text-align:       left;
        }
    </style>
    "@
    
    $days = "monday", "tuesday", "wednesday"
    1..4 |
        ForEach-Object {
        [pscustomobject]@{
            Day         = $_
            ScheduleDay = $days -join "!BREAK!"
        }
    } | ConvertTo-Html -Head $h |
        ForEach-Object{
            $l = $_ -replace "!BREAK!",'<br/>'  # get rid of phony and replace with real break
            $l
        } |
            Out-File "c:\junk\test.html"
    
    c:\junk\test.html
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.