To convert a powershell output file to a CSV or HTML file

Kalaimani Thirupathi 411 Reputation points
2023-05-17T05:04:27.37+00:00

Dear All,

I am having difficulty formatting the ssh output to CSC or HTML. Could you please assist me?

User's image

Here is the output that I am getting and need to convert into the following table: I would appreciate your assistance with the PowerShell query

User's image

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,553 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,596 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 34,431 Reputation points
    2023-05-17T14:29:17.96+00:00

    One way is to parse it by substrings. You may need to adjust the columns and string lengths.

    $data = Get-Content C:\temp\Test.txt
    $csv = foreach ($line in $data) {
        '"{0}","{1}","{2}"' -f $line.Substring(0,4),  $line.Substring(5,26) , $line.Substring(32)
    }
    ""
    $csv 
    
    $html =  foreach ($line in $data) {
        '<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>' -f $line.Substring(0,4),  $line.Substring(5,26) , $line.Substring(32)
    } 
    ""
    "<table>" 
    $html
    "</table>"
    
    
    
    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 46,811 Reputation points
    2023-05-17T19:15:11.8033333+00:00

    A bit more than the answer from the one @MotoX80 gave, but it maintains the column names in the file:

    $oneshot = $true
    $h = [ordered]@{}
    $k = @()
    $keycount = 0
    $node = "\d+"                   # match "Node"
    $dt = "\d{4}-\d{2}-\d{2}"       # match a date
    $tm = "\d{2}:\d{2}:\d{2}\s\w+"  # match a time
    Get-Content C:\junk\test.txt |
        ForEach-Object{
            if ($oneshot){
                ($_ -split " -").trim("-").trim() |
                    ForEach-Object{
                        $h[$_] = ""     # create a key and empty value in the hash
                        $k += $_        # remember the name of the key
                        $keycount++     # and count the number of keys
                    }
                $oneshot = $false
            }
            else{
                if ($_ -match "^\s*($node)\s($dt\s$tm)\s($dt\s$tm)"){
                    for ($i=0;$i -lt $keycount; $i++){
                        $h.($k[$i]) = $matches[$i+1]        # update the key in the hash with a new value
                    }
                    [PSCustomObject]$h
                }
            }
    #    } | Export-Csv c:\junk\test.csv -NoTypeInformation     # create a CSV file
        } | ConvertTo-HTML | Out-File c:\junk\test.html         # create a HTML file
    
    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.