Powershell scipt to conver taxt file to HTML

Kalaimani Thirupathi 411 Reputation points
2021-06-17T16:49:07.847+00:00

Dear All,

I'm not able to convert the PowerShell report/ text file to HTML, getting the only length. can you someone help with this.

example the below one trying to convert to HTML

------------- System Capacity (MiB) -------------
Total Capacity : 937426944
Allocated : 743172096
Legacy Volumes : 0
User : 0
Snapshot : 0
CPGs (VVs) : 671330304
Shared : 69718601
Private : 570563757
Base : 559884471
Reserved : 559884471
Reserved (vSphere vVols) : 0
Snap : 10679286
Reserved : 10679286
Reserved (vSphere vVols) : 0
Free : 31047946
Unmapped : 0
System : 71841792
Internal : 2729984
Admin : 3575808
Spare : 65536000
Used : 0
Unused : 65536000
Free : 194254848
Initialized : 194254848
Uninitialized : 0
Unavailable : 0
Failed : 0
------------------ Efficiency -------------------
Compaction : 4.79
Dedup : 1.17
Compression : 1.19
Data Reduction : 1.36
Overprovisioning : 1.70

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-06-18T04:49:31.387+00:00

    Hi,

    As $3parCapacity is a string array, the ConvertTo-Html cmdlet only gets the Length property and converts it to HTML. You can covert $3parCapacity to some custom objects.

    $htmlfile = "C:\temp\output.html"  
    $table=@()  
    $3parCapacity | Where-Object{ $_ -match ":"} | ForEach-Object {   
        $field,$value = $_ -split ":"  
        $table += [pscustomobject]@{  
        'field' = $field   
        'value' = $value}  
    }  
    $table | ConvertTo-Html | Out-File -FilePath $htmlfile  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


3 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-06-17T17:19:43.14+00:00

    Hi @Kalaimani Thirupathi ,

    I am not sure what you mean with "getting the only length".

    But maybe this helps to get started:

    $a = @"  
    ------------- System Capacity (MiB) -------------  
    Total Capacity : 937426944  
    Allocated : 743172096  
    Legacy Volumes : 0  
    User : 0  
    Snapshot : 0  
    CPGs (VVs) : 671330304  
    Shared : 69718601  
    Private : 570563757  
    Base : 559884471  
    Reserved : 559884471  
    Reserved (vSphere vVols) : 0  
    Snap : 10679286  
    Reserved : 10679286  
    Reserved (vSphere vVols) : 0  
    Free : 31047946  
    Unmapped : 0  
    System : 71841792  
    Internal : 2729984  
    Admin : 3575808  
    Spare : 65536000  
    Used : 0  
    Unused : 65536000  
    Free : 194254848  
    Initialized : 194254848  
    Uninitialized : 0  
    Unavailable : 0  
    Failed : 0  
    ------------------ Efficiency -------------------  
    Compaction : 4.79  
    Dedup : 1.17  
    Compression : 1.19  
    Data Reduction : 1.36  
    Overprovisioning : 1.70  
    "@  
      
    $title = 'My HTML page'  
    $html = @"  
    <html>  
    <head><title>$title</title></head>  
    <body>  
    <pre>$a</pre>  
    </body>  
    </html>  
    "@  
      
    $html | Out-File C:\Temp\b.html  
    

    ----------

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

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 47,901 Reputation points
    2021-06-17T21:36:43.293+00:00

    This isn't the most efficient (and certainly not the prettiest) way to handle this, but it does produce HTML tables:

    $t = @"
    ------------- System Capacity (MiB) -------------
    Total Capacity : 937426944
    Allocated : 743172096
    Legacy Volumes : 0
    User : 0
    Snapshot : 0
    CPGs (VVs) : 671330304
    Shared : 69718601
    Private : 570563757
    Base : 559884471
    Reserved : 559884471
    Reserved (vSphere vVols) : 0
    Snap : 10679286
    Reserved : 10679286
    Reserved (vSphere vVols) : 0
    Free : 31047946
    Unmapped : 0
    System : 71841792
    Internal : 2729984
    Admin : 3575808
    Spare : 65536000
    Used : 0
    Unused : 65536000
    Free : 194254848
    Initialized : 194254848
    Uninitialized : 0
    Unavailable : 0
    Failed : 0
    ------------------ Efficiency -------------------
    Compaction : 4.79
    Dedup : 1.17
    Compression : 1.19
    Data Reduction : 1.36
    Overprovisioning : 1.70
    "@
    
    $ta = $t -split "\r\n"  # convert the string to an array of strings
                            # this should only be necessary for this example
    $th = [ordered]@{}      # used to create a row of table data
    $f = ''                 # hold the HTML fragments
    $tables = ''            # hold the array of HTML to create the tables
    $rows = @()             # hold each table row as a PSCustomObject
    $ta | 
        ForEach-Object{
            if ($_ -match "^-+ (.+) -+$"){
                $nexttablename = $matches[1]    # keep the table name
                if ($rows.count){               # produce table with caption if the last table hasn't been converted to HTML
                    $f = $rows | ConvertTo-Html -Fragment
                    if ($f[0] -match "^(\<table.*?\>)$"){   # add a caption for the table (this clobbers the last table name)
                        $f[0] = "{0} <caption>{1}</caption>" -f $f[0],$tablename    # uses the table name from one under construction
    
                    }
                    $tables += $f               # add the HTML fragment to the string
                }
                # start new table
                $tablename = $nexttablename     # use the saved table name
                $rows = @()
            }
            else{
                # process each row
                $p = $_ -split ":"
                $th['Thing'] = $p[0].trim()     # adjust the column name as appropriate
                $th['Quantity'] = $p[1].trim()  # ditto
                $rows += [PSCustomObject]$th
            }
        }
        # handle the last table in the input data
        if ($rows.count){        # produce table with caption
            $f = $rows | ConvertTo-Html -Fragment
            if ($f[0] -match "^(\<table.*?\>)$"){
                $f[0] = "{0} <caption>{1}</caption>" -f $f[0],$tablename
            }
            $tables += $f
        }
    $title = 'Report in HTML format'
    $report = @"
    <html>
      <head>
        <title>$title</title>
      </head>
      <body>
        $tables
      </body>
    </html>
    "@
    $report | out-file c:\junk\report.html
    

  3. Kalaimani Thirupathi 411 Reputation points
    2021-06-18T02:43:22.437+00:00

    106757-html.jpg


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.