Common/Public/Get-ConsoleText.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function Get-ConsoleText
{
    # Check the host name and exit if the host is not the Windows PowerShell console host.
    if ($host.Name -eq 'Windows PowerShell ISE Host')
    { 
        $psISE.CurrentPowerShellTab.ConsolePane.Text
    }
    elseif ($host.Name -eq 'ConsoleHost')
    {
        $textBuilderConsole = New-Object System.Text.StringBuilder
        $textBuilderLine = New-Object System.Text.StringBuilder

        # 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), $bufferHeight)
        $buffer = $host.UI.RawUI.GetBufferContents($rec) 

        # Iterate through the lines in the console buffer.
        for ($i = 0; $i -lt $bufferHeight; $i++) 
        { 
            for ($j = 0; $j -lt $bufferWidth; $j++) 
            { 
                $cell = $buffer[$i, $j] 
                $null = $textBuilderLine.Append($cell.Character)
            }
            $null = $textBuilderConsole.AppendLine($textBuilderLine.ToString().TrimEnd())
            $textBuilderLine = New-Object System.Text.StringBuilder
        }

        $textBuilderConsole.ToString()
        Write-Verbose "$bufferHeight lines have been copied to the clipboard"
    }
}