2011-08-03 33 views
7

Tôi đã bắt đầu sử dụng bảng điểm bắt đầu trong tiểu sử của mình để lưu nhật ký mọi thứ tôi thực hiện qua trình bao.PowerShell: đọc nhật ký Bản ghi PowerShell

nó trở nên hữu ích để xem lại những thay đổi được thực hiện và khi chúng được tạo. Tôi cũng bắt đầu sử dụng nó như là các bước đầu tiên của tài liệu. Tôi đã bình luận những điều được thực hiện trong trình bao để tham khảo trong tương lai.

Điều đang minh chứng khó khăn là định dạng của tài liệu văn bản và không dễ đọc như lớp vỏ (chủ yếu là màu lỗi, tiết và cảnh báo).

Tôi đã tự hỏi liệu có ai sử dụng chức năng Bản sao theo cách này và có trình xem tùy chọn hoặc tập lệnh phân tích tệp nhật ký để tạo tài liệu thuộc loại nào đó không?

Edit: Tôi muốn biết lý do tại sao các câu hỏi đã được xuống bình chọn ...

+0

Thêm một khi tôi có một vấn đề tương tự hiện nay quá, tôi muốn để có thể thấy được cách ve trong một tài liệu RTF hoặc HTML như đầu ra chứ không phải là chỉ là văn bản thuần túy - xem mọi thứ rõ ràng hơn một chút khi xem xét –

+0

Tôi khá chắc chắn rằng không có cách nào để thực hiện điều này với bảng điểm. Vì tôi đã nói vậy, bạn chắc chắn sẽ nhận được phản hồi ngay bây giờ;) Màu được xử lý chỉ bởi bảng điều khiển (hoặc IDE). Đây là lý do tại sao 'Write-Host' bao gồm' -ForegroundColor' và '-BackgroundColor', nhưng' Write-Output' thì không. –

Trả lời

4

tôi tin rằng nó sẽ rất khó có thể phân tích một bảng điểm để tạo ra một tài liệu định dạng chính xác. Tuy nhiên, bạn có thể sử dụng API máy chủ bàn điều khiển để chụp (các phần của) bộ đệm màn hình.

This Bài viết blog của Windows Powershell mô tả cách hoạt động của tính năng này.

Một cách tầm thường để sử dụng tập lệnh (sửa đổi) (Get-ConsoleAsHtml.ps1) là sửa đổi chức năng nhắc của bạn, để tất cả các dòng từ bộ đệm chưa được ghi vào bảng điểm html của bạn, được lưu mọi thời gian hàm nhắc được gọi. Khối mã đầu tiên là nội dung của tập lệnh được sửa đổi, khối mã thứ hai cho thấy cách bạn có thể sử dụng tập lệnh này trong tiểu sử của mình.

########################################################################################################### 
# 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() 

Ví dụ về một cấu hình:

############################################################################################################ 
# 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)) " 
} 
+0

ý tưởng thú vị đó, tôi sẽ có một đi với nó sớm – Matt

+0

CẢM ƠN! điều này làm việc hoàn hảo! –

Các vấn đề liên quan