Skip to content

Instantly share code, notes, and snippets.

@devlead
Last active January 30, 2024 22:24
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save devlead/2e006ec4a7e134cf38c0 to your computer and use it in GitHub Desktop.
Save devlead/2e006ec4a7e134cf38c0 to your computer and use it in GitHub Desktop.
ConsoleBufferToHtml.ps1 - Powershell script to dump console buffer as html to file.

ConsoleBufferToHtml.ps1

Synopsis

This is a Powershell script to dump console buffer as html to file.

Syntax

ConsoleBufferToHtml.ps1 [-FilePath] [[-Encoding] ] [[-Last] ] [-SkipLast]

Description

This Powershell script will iterate over the current console buffer and output it as html preserving colors.

Parameters

-FilePath

Specifies the path to the output file.

    Required?                    true
    Position?                    1
    Default value
    Accept pipeline input?       false
    Accept wildcard characters?  false

-Encoding

Specifies the type of character encoding used in the file. Valid values are "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode"

    Required?                    false
    Position?                    2
    Default value                UTF8
    Accept pipeline input?       false
    Accept wildcard characters?  false

-Last

Specifies the rows to output from the end of the buffer

    Required?                    false
    Position?                    3
    Default value                0
    Accept pipeline input?       false
    Accept wildcard characters?  false

-SkipLast [<SwitchParameter>]

Skips last buffer row in output

    Required?                    false
    Position?                    named
    Default value                False
    Accept pipeline input?       false
    Accept wildcard characters?  false

Sample usage

ConsoleBufferToHtml.ps1 -FilePath C:\temp\cakebuildlog.htm -Last 50

Cake screenshot

Will output cakebuildlog.htm

<body>
<pre style="display: inline-block;padding:2px;border:2px solid black;font-size:10px;font-family: Consolas, 'Lucida Console', Monaco, monospace;color:#FFFFFF;background-color:#012456"><span style="color:#878E98">Adding file: </span>Cake.Common.dll
<span style="color:#878E98">Adding file: </span>Cake.Common.xml
<span style="color:#878E98">Adding file: </span>Cake.Core.dll
<span style="color:#878E98">Adding file: </span>Cake.Core.pdb
<span style="color:#878E98">Adding file: </span>Cake.Core.xml
<span style="color:#878E98">Adding file: </span>Cake.exe
<span style="color:#878E98">Adding file: </span>LICENSE
<span style="color:#878E98">Adding file: </span>Nuget.Core.dll
<span style="color:#878E98">Adding file: </span>README.md
<span style="color:#878E98">Adding file: </span>ReleaseNotes.md
<span style="color:#878E98">Zip successfully created: </span>C:/temp/dev/gh/cake-build/cake/build/v0.4.2/Cake-bin-v0.4.2.zip
<span style="color:#878E98">Finished executing task: </span>Zip-Files
========================================
Create-NuGet-Packages
========================================
<span style="color:#878E98">Executing task: </span>Create-NuGet-Packages
Attempting to build package from 'Cake.temp.nuspec'.
Successfully created package 'C:/temp/dev/gh/cake-build/cake/build/v0.4.2/nuget\Cake.0.4.2.nupkg'.
Attempting to build package from 'Cake.Core.temp.nuspec'.
Successfully created package 'C:/temp/dev/gh/cake-build/cake/build/v0.4.2/nuget\Cake.Core.0.4.2.nupkg'.
<span style="color:#878E98">Finished executing task: </span>Create-NuGet-Packages
========================================
Package
========================================
<span style="color:#878E98">Executing task: </span>Package
<span style="color:#878E98">Finished executing task: </span>Package
========================================
Default
========================================
<span style="color:#878E98">Executing task: </span>Default
<span style="color:#878E98">Finished executing task: </span>Default
<span style="color:#00FF00">Task Duration </span>
<span style="color:#00FF00">--------------------------------------------------</span>
<span style="color:#00FF00">Clean 00:00:00.0131411 </span>
<span style="color:#00FF00">Restore-NuGet-Packages 00:00:00.2135230 </span>
<span style="color:#00FF00">Patch-Assembly-Info 00:00:00.0087232 </span>
<span style="color:#00FF00">Build 00:00:02.9611817 </span>
<span style="color:#00FF00">Run-Unit-Tests 00:00:19.3848391 </span>
<span style="color:#00FF00">Copy-Files 00:00:00.0451910 </span>
<span style="color:#00FF00">Zip-Files 00:00:00.1987294 </span>
<span style="color:#00FF00">Create-NuGet-Packages 00:00:00.6577465 </span>
<span style="color:#00FF00">Package 00:00:00.0051143 </span>
<span style="color:#00FF00">Default 00:00:00.0049525 </span>
<span style="color:#00FF00">--------------------------------------------------</span>
<span style="color:#00FF00">Total: 00:00:23.4931418 </span>
C:\temp\dev\gh\cake-build\cake<span style="color:#FFFF00"> [</span><span style="color:#00FFFF">develop</span><span style="color:#FFFF00">]</span>&gt; C:\temp\ConsoleBufferToHtml.ps1 -FilePath C:\temp\cakebuildlog.htm -Last 50
</pre>
</body>
<#
.SYNOPSIS
This is a Powershell script to dump console buffer as html to file.
.DESCRIPTION
This Powershell script will iterate over the current console buffer and
output it as html preserving colors.
.PARAMETER FilePath
Specifies the path to the output file.
.PARAMETER Encoding
Specifies the type of character encoding used in the file. Valid values are "Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode", default is UTF8
.PARAMETER Last
Specifies the rows to output from the end of the buffer
.PARAMETER SkipLast
Skips last buffer row in output
#>
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({ ![String]::IsNullOrWhiteSpace($_) })]
[string] $FilePath,
[ValidateSet("Unicode", "UTF7", "UTF8", "UTF32", "ASCII", "BigEndianUnicode")]
[string] $Encoding = "UTF8",
[int] $Last = 0,
[switch]$SkipLast
)
Function ToHex ([System.ConsoleColor] $color)
{
switch($color)
{
"Black" { "#000000" }
"DarkBlue" { "#012456" }
"DarkGreen" { "#005711" }
"DarkCyan" { "#007680" }
"DarkRed" { "#6F0711" }
"DarkMagenta" { "#6F0780" }
"DarkYellow" { "#888F11" }
"Gray" { "#878E98" }
"DarkGray" { "#666D77" }
"Blue" { "#0000FF" }
"Green" { "#00FF00" }
"Cyan" { "#00FFFF" }
"Red" { "#FF0000" }
"Magenta" { "#FF00FF" }
"Yellow" { "#FFFF00" }
"White" { "#FFFFFF" }
default { throw "Unknown color: $color" }
}
}
if ($host.Name -ne "ConsoleHost")
{
throw "Console host $($host.Name) not supported";
}
$state = @{
Width = $host.ui.rawui.BufferSize.Width
Height = if($SkipLast.IsPresent) { $host.ui.rawui.CursorPosition.Y - 1 } else { $host.ui.rawui.CursorPosition.Y }
Rect = (new-object System.Management.Automation.Host.Rectangle 0,0, ($host.ui.rawui.BufferSize.Width-1), $host.ui.rawui.CursorPosition.Y)
DefaultBackgroundColor = [System.ConsoleColor]::DarkMagenta
DefaultForegroundColor = [System.ConsoleColor]::White
}
$buffer = $host.ui.rawui.GetBufferContents($state.Rect)
$currentForegroundColor = $state.DefaultForegroundColor
$currentBackgroundColor = $state.DefaultBackgroundColor
$outputBuilder = new-object System.Text.StringBuilder
$inSpan = $false;
[void]$outputBuilder.AppendLine("<body>")
[void]$outputBuilder.Append("<pre style=`"display: inline-block;padding:2px;border:2px solid black;font-size:10px;font-family: Consolas, 'Lucida Console', Monaco, monospace;color:$(ToHex([System.ConsoleColor]::White));background-color:$(ToHex([System.ConsoleColor]::DarkBlue))`">")
$firstRow = 0
if($Last -gt 0 -and ($state.Height -$Last) -gt 0)
{
$firstRow = $state.Height - $Last;
}
for ($row=$firstRow; $row -lt $state.Height; $row++)
{
for($col = 0; $col -lt $state.Width; $col++)
{
$cell = $buffer[$row, $col]
if ($currentForegroundColor -ne $cell.ForegroundColor -or $currentBackgroundColor -ne $cell.BackgroundColor)
{
$currentForegroundColor = $cell.ForegroundColor
$currentBackgroundColor = $cell.BackgroundColor
if ($inSpan)
{
[void]$outputBuilder.Append("</span>")
$inSpan = $false
}
if (($currentForegroundColor -ne $state.DefaultForegroundColor -and $currentForegroundColor -ne [System.ConsoleColor]::DarkYellow) -or $currentBackgroundColor -ne $state.DefaultBackgroundColor)
{
[void]$outputBuilder.Append("<span style=`"")
if ($currentForegroundColor -ne $state.DefaultForegroundColor -and $currentForegroundColor -ne [System.ConsoleColor]::DarkYellow)
{
[void]$outputBuilder.Append("color:$(ToHex($currentForegroundColor))")
}
if ($currentBackgroundColor -ne $state.DefaultBackgroundColor)
{
[void]$outputBuilder.Append(";background-color:$(ToHex($currentBackgroundColor))")
}
[void]$outputBuilder.Append("`">")
$inSpan = $true
}
}
switch($cell.Character)
{
"<" { [void]$outputBuilder.Append("&lt;") }
">" { [void]$outputBuilder.Append("&gt;") }
default { [void]$outputBuilder.Append($cell.Character) }
}
}
for ($index = $outputBuilder.Length-1; $outputBuilder.Length -gt 0 -and [String]::IsNullOrWhiteSpace($outputBuilder[$index]); $index--)
{
[void]$outputBuilder.Remove($index, 1);
}
if ($inSpan)
{
[void]$outputBuilder.Append("</span>")
$inSpan = $false;
}
[void]$outputBuilder.AppendLine()
}
[void]$outputBuilder.AppendLine("</pre>")
[void]$outputBuilder.AppendLine("</body>")
$outputBuilder.ToString()|Out-File -FilePath $FilePath -Encoding $Encoding
MIT License
Copyright (c) 2022 Mattias Karlsson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment