Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.
Microsoft cloud engineer - SharePoint, Office 365, Azure, DotNet, Angular, JavaScript.

Colorize PowerShell Transcript with CSS

I use Start-Transcript and Stop-Transcript on nearly all PS1 files I create.   It’s a great way to troubleshoot and easily make detailed log files.   PowerShell gives us Write-Host -ForegroundColor to make console output look snazzy with color.   However, TXT output is flat by comparison without color coding.

Below I have sample code for adding <STYLE> and <DIV> tags to PowerShell Write-Host.  This example uses Get-Process to highlight any process using more than 50MB RAM in yellow and 500MB RAM in red.

After renaming TXT to HTM, we can see with Internet Explorer highlights from Transcript logs after the script runs.  Cool beans!  Please leave a comment if you found this helpful.  

shades_smile

 

Code

# functions
function Get-TranscriptFilePath {
	try {
		$externalHost = $host.gettype().getproperty("ExternalHost",[reflection.bindingflags]"nonpublic,instance").getvalue($host, @())
		$externalhost.gettype().getfield("transcriptFileName", "nonpublic,instance").getvalue($externalhost)
    } catch {
		Write-Warning "This host does not support transcription."
    }
}
# main script
Start-Transcript
$proc = Get-Process |? {$_.WS -gt 10MB}
foreach ($p in $proc) {
	$ws=$p.WS
	$pn=$p.ProcessName
	$c="White"
	if ($ws -gt 50MB) {$c="yellow"}
	if ($ws -gt 500MB) {$c="red"}
	$html="
$ws,$pn
" Write-Host $html -ForegroundColor $c } $tran = Get-TranscriptFilePath Stop-Transcript # format TXT transcript as HTM $filename = $tran.Replace(".txt",".htm") $header = "`n`n`n`n`n" $txt = Get-Content $tran $footer = "`n`n" "$header $txt $footer" | Out-File $filename # launch IE to view HTM start iexplore.exe $filename

 

 

Screenshots

image
color

© Copyright 2016
@ SPJeff

Return to Top ▲Return to Top ▲