question

matteu31400 avatar image
0 Votes"
matteu31400 asked RichMatheisen-8856 commented

Array html and skip line

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @matteu31400 ,

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


image.png (15.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

matteu31400 avatar image
0 Votes"
matteu31400 answered cooldadtx commented

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



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

True. ConvertTo-Html is very limited. The general workaround to issues with generated HTML is to do post-conversion. In this case after you get the string back from the conversion do a replace on &lt;br/&gt; to <br/>.

0 Votes 0 ·
matteu31400 avatar image
0 Votes"
matteu31400 answered

Hello,

Thanks for your answer. It works perfectly :)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

matteu31400 avatar image
0 Votes"
matteu31400 answered RichMatheisen-8856 commented

Yes, thank you.

I just asked for what I need help but if you can give me some advices, I would appreciate.
this is how I built my script (I didn't write all because it's 2600 lines )

I make function and create custom object and then build my html file.

 function Get-veeamSQLInventory
 {
     Write-host "VeeamSQLInventory"
     $SQLSettings = (Get-ItemProperty "HKLM:\SOFTWARE\Veeam\Veeam Backup and Replication")
     $instance = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
     $RegistryKey = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$instance
     $Edition = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$RegistryKey\Setup").Edition.split(" ")[0]
     [PScustomObject]@{
         SQLServerName = $SQLSettings.SqlServerName
         SQLEdition = $Edition
         SQLInstanceName = $SQLSettings.SqlInstanceName
         SQLDatabaseName = $SQLSettings.SqlDatabaseName
         SQLServiceAccount = (Get-WmiObject win32_service -ComputerName $SQLSettings.SqlServerName -Filter {name like "%mssql%"}).startname
     }
     Write-host "-------------------"
 }
    
       
 function Get-VeeamRBAC
 {
     Write-host "RBAC"
    
     $RBACreq = Get-VBRUserRoleAssignment
     foreach ($item in $RBACreq)
     {
         [pscustomobject]@{
             Name = $item.name
             Type = $item.type
             Role = $item.role
         }
     }
     Write-host "-------------------"
 }
    
 function Get-VeeamConfigurationBackup
 {
     Write-host "Configuration Backup"
    
     $ConfigurationBackupReq = Get-VBRConfigurationBackupJob
     [pscustomobject]@{
         Enabled = $ConfigurationBackupReq.Enabled
         Repository = $ConfigurationBackupReq.Target
         ScheduleOptions = $ConfigurationBackupReq.ScheduleOptions
         RestorePointsToKeep = $ConfigurationBackupReq.RestorePointsToKeep
         EncryptionOptions = $ConfigurationBackupReq.EncryptionOptions
         LastResult = $ConfigurationBackupReq.LastResult
     }
     Write-host "-------------------"
 }
    
    
 #HTML Building
 #Write here all function that need to be displayed in all reports types
 $VeeamSQLInventory = Get-VeeamSQLInventory
 $VeeamRBAC = Get-VeeamRBAC
 $VeeamConfigurationBackup = Get-VeeamConfigurationBackup
    
 #Function to create Array for HTML
 Function CreateArray ($title,$var)
 {
     "<h3>$title</h3>"
     $var | ConvertTo-Html -Fragment
 }
    
 #region HTML
 @"
 <!DOCTYPE html>
 <html>
 <head>
 <style>
 table {
    # border-collapse: collapse;
    border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;
 }
 h2 {text-align:center}
 th, td {
     #padding: 8px;
     #text-align: left;
     #border-bottom: 1px solid #ddd;
     border-width: 1px; padding: 3px; border-style: solid; border-color: black;
 }
    
 tr:hover{background-color:#f5f5f5}
 </style>
 </head>
 <body>
 <h1>VEEAM Report</h1>
    
 $(CreateArray -title VeeamSQLInventory -var $VeeamSQLInventory)
 $(CreateArray -title VeeamRBAC -var $VeeamRBAC)
 $(CreateArray -title VeeamConfigurationBackup -var $VeeamConfigurationBackup)
    
 </body>
 "@ | Out-File -Encoding utf8 $htmlReportPath
 #endregion
    
 Invoke-Item $htmlReportPath


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Since this question has been answered it's better if you create a new question.

Just by looking at your code I don't see anything that is obviously wrong. Is there a problem? If so, it's a good idea to state what it is.

0 Votes 0 ·