-
-
Save mavaddat/6af2cf8ff48a66779a9a28dbf3d8f45e to your computer and use it in GitHub Desktop.
Colorized capture of console screen in HTML and RTF – http://blogs.msdn.com/b/powershell/archive/2009/01/11/colorized-capture-of-console-screen-in-html-and-rtf.aspx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ConsoleAsHTML | |
{ | |
[CmdletBinding()] | |
param ( | |
) | |
# | |
# The script captures console screen buffer up to the current cursor position and returns it in HTML format. | |
# | |
# Returns: UTF8-encoded string. | |
# | |
# Example: | |
# | |
# $htmlFileName = "$env:temp\ConsoleBuffer.html" | |
# .\Get-ConsoleAsHtml | out-file $htmlFileName -encoding UTF8 | |
# $null = [System.Diagnostics.Process]::Start("$htmlFileName") | |
# | |
begin | |
{ | |
# Check the host name and exit if the host is not the Windows PowerShell console host. | |
if ($host.Name -ne 'ConsoleHost') | |
{ | |
Write-Host -ForegroundColor Red "This script runs only in the console host. You cannot run this script in $($host.Name)." | |
exit -1 | |
} | |
# The Windows PowerShell console host redefines DarkYellow and DarkMagenta colors and uses them as defaults. | |
# The redefined colors do not correspond to the color names used in HTML, so they need to be mapped to digital color codes. | |
# | |
function Set-NormalizeHtmlColor ($color) | |
{ | |
if ($color -eq "DarkYellow") { $color = "#eeedf0" } | |
if ($color -eq "DarkMagenta") { $color = "#012456" } | |
return $color | |
} | |
# Create an HTML span from text using the named console colors. | |
# | |
function Get-HtmlSpan ($text, $forecolor = "DarkYellow", $backcolor = "DarkMagenta") | |
{ | |
$forecolor = Set-NormalizeHtmlColor $forecolor | |
$backcolor = Set-NormalizeHtmlColor $backcolor | |
# You can also add font-weight:bold tag here if you want a bold font in output. | |
return "<span style='font-family:Courier New;color:$forecolor;background:$backcolor'>$text</span>" | |
} | |
# Generate an HTML span and append it to HTML string builder | |
# | |
function Set-HtmlSpan | |
{ | |
$spanText = $spanBuilder.ToString() | |
$spanHtml = Get-HtmlSpan $spanText $currentForegroundColor $currentBackgroundColor | |
$null = $htmlBuilder.Append($spanHtml) | |
} | |
# Append line break to HTML builder | |
# | |
function Set-HtmlBreak | |
{ | |
$null = $htmlBuilder.Append("<br>") | |
} | |
} | |
process | |
{ | |
# Initialize the HTML string builder. | |
$htmlBuilder = New-Object system.text.stringbuilder | |
$null = $htmlBuilder.Append("<pre style='MARGIN: 0in 10pt 0in;line-height:normal';font-size:10pt>") | |
# Grab the console screen buffer contents using the Host console API. | |
$bufferWidth = $host.ui.rawui.BufferSize.Width | |
$bufferHeight = $host.ui.rawui.CursorPosition.Y | |
$rec = New-Object System.Management.Automation.Host.Rectangle 0, 0, ($bufferWidth - 1), $bufferHeight | |
$buffer = $host.ui.rawui.GetBufferContents($rec) | |
# Iterate through the lines in the console buffer. | |
for ($i = 0; $i -lt $bufferHeight; $i++) | |
{ | |
$spanBuilder = New-Object system.text.stringbuilder | |
# Track the colors to identify spans of text with the same formatting. | |
$currentForegroundColor = $buffer[$i, 0].ForegroundColor | |
$currentBackgroundColor = $buffer[$i, 0].BackgroundColor | |
for ($j = 0; $j -lt $bufferWidth; $j++) | |
{ | |
$cell = $buffer[$i, $j] | |
# If the colors change, generate an HTML span and append it to the HTML string builder. | |
if (($cell.ForegroundColor -ne $currentForegroundColor) -or ($cell.BackgroundColor -ne $currentBackgroundColor)) | |
{ | |
Set-HtmlSpan | |
# Reset the span builder and colors. | |
$spanBuilder = New-Object system.text.stringbuilder | |
$currentForegroundColor = $cell.ForegroundColor | |
$currentBackgroundColor = $cell.BackgroundColor | |
} | |
# Substitute characters which have special meaning in HTML. | |
switch ($cell.Character) | |
{ | |
'>' { $htmlChar = '>' } | |
'<' { $htmlChar = '<' } | |
'&' { $htmlChar = '&' } | |
default | |
{ | |
$htmlChar = $cell.Character | |
} | |
} | |
$null = $spanBuilder.Append($htmlChar) | |
} | |
Set-HtmlSpan | |
Set-HtmlBreak | |
} | |
# Append HTML ending tag. | |
$null = $htmlBuilder.Append("</pre>") | |
return $htmlBuilder.ToString() | |
} | |
} | |
Get-ConsoleAsHTML |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment