PowerShell:阅读PowerShell传输日志

7

我开始在我的个人资料中使用start-transcript来记录我通过shell做的所有事情。

这对于回顾所做的更改以及它们何时进行非常有用。我也开始将其用作文档编写的第一步。我已经注释了在shell中完成的工作,以供将来参考。

但是,有一个棘手的问题是格式化方式类似于文本文档,不像shell(主要是错误,详细和警告颜色)那样易于阅读。

我想知道是否有人以这种方式使用转录功能,并且是否有首选的查看器或脚本来解析日志文件以生成某种类型的文档?

编辑:我很想知道为什么这个问题被投票否决了...


因为我现在也有一个类似的问题,所以我想要能够在RTF或HTML文档中看到着色而不仅仅是纯文本格式 - 这样在审查时可以更清晰地看到。 - Ashley
我相当确定使用转录无法完成这个任务。既然我说了这句话,你肯定会得到一个回应 ;)颜色处理仅由控制台(或IDE)处理。这就是为什么Write-Host包括-ForegroundColor-BackgroundColor,但Write-Output没有的原因。 - Tom H
1个回答

4
我认为解析转录文件并创建准确格式的文档将非常困难。但是,您可以使用控制台主机API来捕获(部分)屏幕缓冲区。这篇Windows Powershell博客文章描述了如何操作。这里
一种简单的使用方法是修改提示函数(Get-ConsoleAsHtml.ps1),以便每次调用提示函数时保存尚未写入html转录的缓冲区中的所有行。第一个代码块是修改后的脚本内容,第二个代码块展示了如何在您的个人资料中使用此脚本。
########################################################################################################### 
# Get-ConsoleAsHtml.ps1 
# 
# The script captures console screen buffer up to the current cursor position and returns it in HTML format.
# (Jon Z: Added a startline parameter)
# 
# Returns: UTF8-encoded string. 
# 
# Example: 
# 
# $htmlFileName = "$env:temp\ConsoleBuffer.html" 
# .\Get-ConsoleAsHtml 5 | out-file $htmlFileName -encoding UTF8 
# $null = [System.Diagnostics.Process]::Start("$htmlFileName") 
# 

param (
  $startline = 0
)

# 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 Normalize-HtmlColor ($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 Make-HtmlSpan ($text, $forecolor = "DarkYellow", $backcolor = "DarkMagenta") 
{ 
  $forecolor = Normalize-HtmlColor $forecolor 
  $backcolor = Normalize-HtmlColor $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 Append-HtmlSpan 
{ 
  $spanText = $spanBuilder.ToString() 
  $spanHtml = Make-HtmlSpan $spanText $currentForegroundColor $currentBackgroundColor 
  $null = $htmlBuilder.Append($spanHtml) 
} 

# Append line break to HTML builder 
# 
function Append-HtmlBreak 
{ 
  $null = $htmlBuilder.Append("<br>") 
} 

# 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 = $startline; $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)) 
    { 
      Append-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 = '&gt;' } 
      '<' { $htmlChar = '&lt;' } 
      '&' { $htmlChar = '&amp;' } 
      default 
      { 
        $htmlChar = $cell.Character 
      } 
    } 

    $null = $spanBuilder.Append($htmlChar) 
  } 

  Append-HtmlSpan 
  Append-HtmlBreak 
} 

# Append HTML ending tag. 
$null = $htmlBuilder.Append("</pre>") 

return $htmlBuilder.ToString()

一个示例配置文件:

############################################################################################################ 
# Microsoft.PowerShell_profile.ps1 
# 

$docpath = [environment]::GetFolderPath([environment+SpecialFolder]::MyDocuments)
$transcript = "$($docpath)\PowerShell_transcript.$(get-date -f 'yyyyMMddHHmmss').html";
$global:lastloggedline = 0
function prompt { 
 &'D:\Scripts\Get-ConsoleAsHtml.ps1' $global:lastloggedline | out-file $transcript -append;
 $global:lastloggedline = $host.ui.rawui.cursorposition.Y
 "PS $pwd$('>' * ($nestedPromptLevel + 1)) "
}

有趣的想法,我很快就会尝试一下。 - Matt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接