2011-08-03 16 views
7

पढ़ना मैंने खोल के माध्यम से जो कुछ भी किया है, उसका लॉग रखने के लिए मैंने अपनी प्रोफ़ाइल में स्टार्ट-ट्रांसक्रिप्ट का उपयोग करना शुरू कर दिया है।पावरशेल: पॉवरशेल ट्रांसक्रिप्ट लॉग

यह देखने के लिए उपयोगी हो रहा है कि क्या परिवर्तन किए गए हैं और जब उन्हें बनाया गया था। मैं इसे दस्तावेज़ीकरण के पहले चरण के रूप में भी उपयोग करना शुरू कर रहा हूं। मैं भावी संदर्भ के लिए खोल में किए गए कार्यों पर टिप्पणी कर रहा हूं।

जो चीज मुश्किल साबित कर रही है वह स्वरूपण एक पाठ दस्तावेज़ का है और खोल (त्रुटि, वर्बोज़ और चेतावनी रंग मुख्य रूप से) के रूप में पढ़ने के लिए आसान नहीं है।

मैं सोच रहा था कि कोई इस तरह से ट्रांसक्रिप्ट कार्यक्षमता का उपयोग करता है और वरीयता या एक स्क्रिप्ट का दर्शक है जो लॉग फ़ाइल को किसी प्रकार का दस्तावेज़ बनाने के लिए पार करता है?

संपादित करें: मैं जानना चाहता है कि सवाल नीचे कर दिया गया है मतदान इच्छुक हूँ ...

+0

प्लस एक के रूप में मैं एक ऐसी ही समस्या अब भी है, मैं चाहता हूँ के बजाय आउटपुट के रूप में एक आरटीएफ या HTML दस्तावेज़ में रंगाई को देखने के लिए सक्षम होने के लिए सिर्फ सादा पाठ - –

+0

की समीक्षा करते समय चीजों को थोड़ा स्पष्ट देखें, मैं काफी हद तक निश्चित हूं कि ट्रांसक्रिप्ट के साथ इसे पूरा करने का कोई तरीका नहीं है। चूंकि मैंने यह कहा है, अब आप एक प्रतिक्रिया प्राप्त कर सकते हैं;) रंग केवल कंसोल (या आईडीई) द्वारा संभाला जाता है। यही कारण है कि 'लिखें-होस्ट' में '-ForegroundColor' और' -BackgroundColor' शामिल है, लेकिन 'लिखें-आउटपुट' नहीं है। –

उत्तर

4

मेरा मानना ​​है कि यह एक सटीक स्वरूपित दस्तावेज़ बनाने के लिए एक प्रतिलिपि पार्स करने के लिए बहुत मुश्किल हो जाएगा। हालांकि आप स्क्रीन बफर को कैप्चर करने के लिए कंसोल होस्ट एपीआई का उपयोग कर सकते हैं।

This विंडोज पावरहेल ब्लॉग आलेख वर्णन करता है कि यह कैसे काम करता है।

(संशोधित) स्क्रिप्ट (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)) " 
} 
+0

दिलचस्प विचार है कि, मैं जल्द ही इसके साथ – Matt

+0

धन्यवाद होगा! यह सही काम किया! –